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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步