105-Python中将数据插入字符串

Python中将数据插入字符串

name = 'James'
height = 181.18

# 使用 + 运算符
str_1 = name + ' has a height of ' + str(height) + ' cm.'
print(str_1)
# James has a height of 181.18 cm.

# 使用 % 
str_2 = '%s has a height of %.3f cm.' %(name, height)
print(str_2)
# James has a height of 181.180 cm.

# 使用 str.format()
str_3 = '{} has a height of {:.3f} cm.'.format(name, height)
print(str_3)
# James has a height of 181.180 cm.

# 使用f-strings
str_4 = f'{name} has a height of {height:.3f} cm.'
print(str_4)
# James has a height of 181.180 cm.


posted @ 2024-07-16 17:08  像一棵海草海草海草  阅读(4)  评论(0编辑  收藏  举报