编程语言
首页 > 编程语言> > Python中Print方法

Python中Print方法

作者:互联网

 1 number1 = int(input("请输入第一个数:"))
 2 number2 = int(input("请输入第二个数:"))
 3 
 4 # 方法一:字符串拼接,注意数字需要str()转化为字符串再拼接
 5 print(str(number1) + " + " + str(number2) + " = " + str(number1 + number2))
 6 
 7 # 方法二:f"{变量}"
 8 print(f"{number1} + {number2} = {number1 + number2}")
 9 
10 # 方法三:"%d" %变量
11 print("%d + %d = %d" % (number1, number2, number1 + number2))
12 
13 # 方法四:"{编号}".format(变量)
14 print("{0} + {1} = {2}".format(number1, number2, number1 + number2))
15 # or
16 print("{number1} + {number2} = {number3}".format(number1=number1, number2=number2, number3=number1 + number2))
17 # or
18 print("{} + {} = {}".format(number1, number2, number1 + number2))
19 
20 # 方法五:逗号和sep  
21 print('2019', '12', '12', sep='-') 

_________

1 控制台结果:
2 请输入第一个数:20
3 请输入第二个数:30
4 20 + 30 = 50
5 20 + 30 = 50
6 20 + 30 = 50
7 20 + 30 = 50
8 20 + 30 = 50
9 2019-12-12

 

标签:12,20,Python,30,print,number2,number1,Print,方法
来源: https://www.cnblogs.com/thankcat/p/16553598.html