4.格式化字符串

#格式化字符串:将其他类型数据按照指定格式转换成字符串

"""

1.F-String(Python 3.6+)

    result = f"小明今年{age}岁了!"

2.format()

    result = "小明今年{下标(可空)}岁了!".format(age)

3.%(已弃用)

    result = "小明今年%d岁了!" % (age)

"""

name = "小明"

age = "20"

result = "你好" + name + ",你今年已经 " + str(age) + " 岁了!"

print(result)

#1.F-String

result1 = f"你好{name},你今年已经 {age} 岁了!"

print(result1)

#2.format()

result2 = "你好{0},你今年已经 {1} 岁了!".format(name, age)

print(result2)

#3.%(弃用)

#result3 = "你好%s,你今年已经 %d 岁了!" % (name, age)

你可能感兴趣的:(4.格式化字符串)