015 格式化输出

### 格式化输出
#coding:utf-8

# 方法一
b = 20
c = "张三"
d = "李四"

str = "My name is %s" %c
print(str)

str_two = "My name is %s, age %s" %(d, b)
print(str_two)


# 方法二
str_three = "My name is {} and age is {}".format(c, b)    # 默认位置
print(str_three)

str_four = "My name is {1} and age is {0}".format(b, c)    # 指定位置
print(str_four)


# 方法三
print(f"My name is {c} and age is {b}")


# format
formatter = "{} {} {} {}"
print(formatter.format(
    "Try your",
    "Own text here",
    "Maybe a poem",
    "Or a song about fear"
))
原文地址:https://www.cnblogs.com/huafan/p/13874014.html