1. 使用`%`操作符:
name = "李明"
age = 26
city = "SuZhou"
print("the name is %s, his age is %d, he comes from %s" % (name, age, city))
2. 使用`str.format()`方法:
name = "李明"
age = 26
city = "SuZhou"
print("the name is {}, his age is {}, he comes from {}".format(name, age, city))
3. 使用f字符串(Python 3.6及以上版本支持):
name = "李明"
age = 26
city = "SuZhou"
print(f"the name is {name}, his age is {age}, he comes from {city}")
以上三种方法都可以用来格式化字符串,选择哪一种取决于你的具体需求和Python的版本。f字符串因其简洁和易读性,在Python 3.6及以上版本中成为了首选的格式化方法。