Python在Print()中的格式化

时间:2019-06-11 08:58:00

标签: python

要在Python中打印带有数学加数的字符串,正确的做法是:

# Read two inputs from users
a = input("Enter your First Number")
b = input("Enter your Second Number")

# perform type conversion
a = int(a)
b = int(b)

print (f"Result: {a}+{b}")

输出:

Enter your First Number10
Enter your Second Number10
Result: 10+10

所需的输出:Result: 20

4 个答案:

答案 0 :(得分:1)

您当前的格式字符串Result: {a}+{b}ab分别打印为Result: 10+10,并且不执行加法操作。

要实现此目的,您需要将f-string更改为f"Result: {a+b}",以便在格式大括号内进行添加并打印结果

print (f"Result: {a+b}")

输出将为

Result: 20

答案 1 :(得分:0)

a = input("Enter your First Number")
b = input("Enter your Second Number")

# perform type conversion
a = int(a)
b = int(b)

print (f"Result: {a + b}")

答案 2 :(得分:0)

您应该更改

print (f"Result: {a} + {b}")

print (f"Result: {a + b}")

答案 3 :(得分:-1)

Array.reduce