Linux 拨号vps windows公众号手机端

Python中字符串格式化的操作是什么

lewis 9年前 (2016-10-25) 阅读数 6 #程序编程
文章标签 python

Python中字符串格式化的操作是通过使用字符串的format()方法或者使用%操作符来将变量插入到字符串中。下面是两种字符串格式化的示例:

使用format()方法:

name = "Alice"
age = 30
sentence = "My name is {} and I am {} years old".format(name, age)
print(sentence)

使用%操作符:

name = "Bob"
age = 25
sentence = "My name is %s and I am %d years old" % (name, age)
print(sentence)

这两种方法都可以实现字符串格式化的操作,选择其中一种来使用即可。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门