Loading

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

 

posted @ 2022-08-05 10:51  ThankCAT  阅读(102)  评论(0编辑  收藏  举报