python五种print方法:字符串拼接,f“{变量}“,“%d“ %变量,“{编号}“.format(变量)

number1 = int(input("请输入第一个数:"))
number2 = int(input("请输入第二个数:"))

# 方法一:字符串拼接,注意数字需要str()转化为字符串再拼接
print(str(number1) + " + " + str(number2) + " = " + str(number1 + number2))

# 方法二:f"{变量}"
print(f"{number1} + {number2} = {number1 + number2}")

# 方法三:"%d" %变量
print("%d + %d = %d" % (number1, number2, number1 + number2))

# 方法四:"{编号}".format(变量)
print("{0} + {1} = {2}".format(number1, number2, number1 + number2))
# or
print("{number1} + {number2} = {number3}".format(number1=number1, number2=number2, number3=number1 + number2))
# or
print("{} + {} = {}".format(number1, number2, number1 + number2))

# 方法五:逗号和sep  
print('2019', '12', '12', sep='-') 
控制台结果:
请输入第一个数:20
请输入第二个数:30
20 + 30 = 50
20 + 30 = 50
20 + 30 = 50
20 + 30 = 50
20 + 30 = 50
2019-12-12

你可能感兴趣的:(python,python,开发语言,后端)