Python中的字符串格式化:使用print中的format函数
作者:NorthPath
时间:2023-04-24
来源:互联网
浏览:0
变量插入字符串的方法Python中的format()函数是一种将变量插入字符串的方法,能够使字符串更易于阅读和理解。它支持许多不同的用法,以下是具体的用法和说明:使用位置参数传递变量name='John'age=25print('Mynameis{},andIam{}yearsold.'.format(name,age))#输出:MynameisJohn,andIam25yearsold.使用索引传递变量name='John'age=25print('
变量插入字符串的方法
Python中的format()函数是一种将变量插入字符串的方法,能够使字符串更易于阅读和理解。它支持许多不同的用法,以下是具体的用法和说明:
使用位置参数传递变量
name = 'John'
age = 25
print('My name is {}, and I am {} years old.'.format(name, age))
# 输出:My name is John, and I am 25 years old.使用索引传递变量
name = 'John'
age = 25
print('My name is {0}, and I am {1} years old.'.format(name, age))
# 输出:My name is John, and I am 25 years old.使用关键字参数传递变量
name = 'John'
age = 25
print('My name is {n}, and I am {a} years old.'.format(n=name, a=age))
# 输出:My name is John, and I am 25 years old.格式化数字
price = 19.99
print('The price is ${:.2f}'.format(price))
# 输出:The price is $19.99对齐文本
text = 'Hello'
print('{:>10}'.format(text)) # 右对齐输出,总宽度为10
# 输出: Hello
print('{:^10}'.format(text)) # 居中输出,总宽度为10
# 输出: Hello
print('{:<10}'.format(text)) # 左对齐输出,总宽度为10
# 输出:Hello使用格式化字符串(Python 3.6及以上版本)
name = 'John'
age = 25
print(f'My name is {name}, and I am {age} years old.')
# 输出:My name is John, and I am 25 years old.使用字典传递变量
person = {'name': 'John', 'age': 25}
print('My name is {name}, and I am {age} years old.'.format(**person))
# 输出:My name is John, and I am 25 years old.使用下标操作符获取列表中的元素
fruits = ['apple', 'banana', 'cherry']
print('My favorite fruit is {0[1]}'.format(fruits))
# 输出:My favorite fruit is banana使用花括号转义
print('{{Hello}}'.format()) # 输出:{Hello}使用冒号分隔格式字符串和变量名称,对变量进行进一步格式化
name = 'John'
score = 95
print('Student: {0:<10} Score: {1:.2f}'.format(name, score))
# 输出:Student: John Score: 95.00根据变量类型自动选择格式
x = 42
y = 3.14
print('x is {!r}, y is {!s}'.format(x, y))
# 输出:x is 42, y is 3.14使用填充字符
x = 42
print('{:0>5}'.format(x)) # 右对齐,用 0 填充,总宽度为 5
# 输出:00042根据变量类型选择不同的进制输出
x = 42
print('bin: {0:b}, oct: {0:o}, hex: {0:x}'.format(x))
# 输出:bin: 101010, oct: 52, hex: 2a自定义格式化函数
def format_salary(salary):
if salary > 10000:
return '{:.1f}K'.format(salary / 1000)
else:
return '${:,.2f}'.format(salary)
print(format_salary(5000)) # $5,000.00
print(format_salary(15000)) # 15.0K使用 ** 和 * 进行动态参数传递
data = {'name': 'John', 'age': 25}
print('{name} is {age} years old.'.format(**data)) # John is 25 years old.
fruits = ['apple', 'banana', 'cherry']
print('My favorite fruits are {}, {} and {}.'.format(*fruits)) # My favorite fruits are apple, banana and cherry.
作者最新文章
贵州省住建厅与贝壳集团签署旅居战略合作:五大维度落地方案解析
2026-09-08 18:13
上海链家安住APP:业主主动卖房功能与成交数据解析
2026-09-08 18:11
如何批量将PPT转成PDF格式?PPT转PDF工具怎么选?
2026-09-04 16:03
PDF文件怎么压缩?3个小技巧帮你减小体积
2026-09-03 18:03
小批量试产总结报告:新产品量产导入评审实战指南
2026-09-02 19:48
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















