Python中的字符串格式化方式:format()函数的使用方法

王林
发布: 2023-04-22 19:01:05
转载
2140人浏览过

变量插入字符串的方法

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.
登录后复制

以上就是Python中的字符串格式化方式:format()函数的使用方法的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:亿速云网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号