1.%s
print("%s_%s_%s" %(“as”,“qw”,“er”))
2.f-strings 是指以 f 或 F 开头的字符串,其中以 {} 包含的表达式会进行值替换。
- 基本使用
>>> name = 'flhw'
>>> age = 18
>>> f"hi, {name}, are you {age}"
'hi, flhw, are you 18'
>>> F"hi, {name}, are you {age}"
'hi, flhw, are you 18'
- 运算表达式
>>> f"{ 2 * 2 + 1}"
'5'
- 调用函数
>>> def test(input):
... return input.upperr()
...
>>> name = "flhw"
>>> f"{test(name)} is handsome."
'FLHW is handsome.
3.name.format()
name = “alex{},{},{}”
print(name.format(1,2,3)) # 按照顺序位置进行填充
name = “alex{2},{0},{1}”
print(name.format(“a”,“b”,“c”)) # 按照索引值进行填充
name = “alex{a},{b},{c}”
print(name.format(a=1,c=11,b=67)) # 按照关键字进行填充