Python中字符串的Format用法。
Python,字符串,Format,用法
2025-04-07 09:01:26 时间
大家好,又见面了,我是你们的朋友全栈君。
一、例子:
"_".join(["1","2","3","4"])
"_".join(map(lambda x:str(x),[1,2,3,4]))
"{0}-{1}".format(3.4,34)
"{}-{}-{}".format(3,4,5)
"{name}-{age}".format(**{'name':'song','age':34})
"{name}-{age}".format(name='song',age=34)
"the list first is: {obj[0]}".format(obj=[1,2,3,4])
"the list first is: {0[0]},{0[1]}".format([1,2,3,4])
"{:>8}".format('abc')
"{0:<8}".format('abc')
"{0:_<8}".format('abc')
"{0:0>8}".format('3.14')
"{0:.2f}".format(123.123456)
"{0:b}".format(1023)
"{0:d}".format(0b1111111111)
"{0:x}".format(1023)
"{0:,}".format(102345.6789)
二、结果:
>>> "_".join(["1","2","3","4"])
'1_2_3_4'
>>> "_".join(map(lambda x:str(x),[1,2,3,4]))
'1_2_3_4'
>>> "{0}-{1}".format(3.4,34)
'3.4-34'
>>> "{}-{}-{}".format(3,4,5)
'3-4-5'
>>> "{name}-{age}".format(**{'name':'song','age':34})
'song-34'
>>> "{name}-{age}".format(name='song',age=34)
'song-34'
>>> "the list first is: {obj[0]}".format(obj=[1,2,3,4])
'the list first is: 1'
>>> "the list first is: {0[0]},{0[1]}".format([1,2,3,4])
'the list first is: 1,2'
>>> "{:>8}".format('abc')
' abc'
>>> "{0:<8}".format('abc')
'abc '
>>> "{0:_<8}".format('abc')
'abc_____'
>>> "{0:0>8}".format('3.14')
'00003.14'
>>> "{0:.2f}".format(123.123456)
'123.12'
>>> "{0:b}".format(1023)
'1111111111'
>>> "{0:d}".format(0b1111111111)
'1023'
>>> "{0:x}".format(1023)
'3ff'
>>> "{0:,}".format(102345.6789)
'102,345.6789'
>>>
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/156075.html原文链接:https://javaforall.cn
相关文章
- python批量获取同程酒店的信息
- python lasso回归分析_解析python实现Lasso回归「建议收藏」
- Python基本数据类型有哪些
- 用Python爬取王者荣耀英雄皮肤海报图
- 【InventWithPython 第一部分】校对活动正式启动
- 基于python的安全帽识别安全帽检测可以检测图片,视频流,有界面[通俗易懂]
- 用Python的turtle库画太极图
- python提取xml指定内容
- 快过年了,用Python写副春联&福字送给你~
- python小知识点-exec的local
- python win32api messagebox_如何在Python中使用Win32 API?
- Python基础12-内置函数
- Python爬取书包网文章实战总结
- python实现樱花[通俗易懂]
- 建议收藏!Python 读取千万级数据自动写入 MySQL 数据库
- Python递归实现全排列
- Python负数取余总结
- 用 Python 破解 WiFi 密码,太刺激了!
- Python基础23-SSH模块paramiko
- X波段双极化相控阵天气雷达基数据的python读取方法