Python入门教程(三十三)Python的字符串格式化

这篇文章主要介绍了Python入门教程(三十三)Python的字符串格式化,为了确保字符串按预期显示,我们可以使用 format()方法对结果进行格式化,需要的朋友可以参考下

为了确保字符串按预期显示,我们可以使用 format() 方法对结果进行格式化。

字符串 format()

format() 方法允许您格式化字符串的选定部分。

有时文本的一部分是你无法控制的,也许它们来自数据库或用户输入?

要控制此类值,请在文本中添加占位符(花括号 {}),然后通过 format() 方法运行值:

实例

添加要显示价格的占位符:

price = 52 txt = "The price is {} dollars" print(txt.format(price))

运行实例

你可以在花括号内添加参数以指定如何转换值

实例

将价格格式化为带有两位小数的数字:

txt = "The price is {:.2f} dollars"

运行实例

price = 52 txt = "The price is {:.2f} dollars" print(txt.format(price))

查看字符串 format() 参考手册中的所有格式类型。

多个值

如需使用更多值,只需向 format() 方法添加更多值:

print(txt.format(price, itemno, count))

并添加更多占位符

实例

quantity = 3 itemno = 567 price = 52 myorder = "I want {} pieces of item number {} for {:.2f} dollars." print(myorder.format(quantity, itemno, price))

运行实例

索引号

您可以使用索引号(花括号 {0} 内的数字)来确保将值放在正确的占位符中:

实例

quantity = 3 itemno = 567 price = 52 myorder = "I want {0} pieces of item number {1} for {2:.2f} dollars." print(myorder.format(quantity, itemno, price))

运行实例

此外,如果要多次引用相同的值,请使用索引号:

实例

age = 63 name = "Bill" txt = "His name is {1}. {1} is {0} years old." print(txt.format(age, name))

运行实例

命名索引

您还可以通过在花括号 {carname} 中输入名称来使用命名索引,但是在传递参数值 txt.format(carname = “Ford”) 时,必须使用名称:

实例

myorder = "I have a {carname}, it is a {model}." print(myorder.format(carname = "Porsche", model = "911"))

运行实例

到此这篇关于Python入门教程(三十三)Python的字符串格式化的文章就介绍到这了,更多相关Python字符串格式化内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是Python入门教程(三十三)Python的字符串格式化的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python