在 不带括号的情况下 使用 print函数 可用于旧版本的Python,但 Python3不再支持该功能
,因此您必须将参数放在括号内。但是,有一些变通方法,如对该问题的答案所述。由于对Python2的支持已于2020年1月1日结束,因此
答案已修改为与Python3兼容 。
您可以执行以下任一操作(并且可能还有其他方法):
(1) print("First number is {} and second number is {}".format(first, second))(1b) print("First number is {first} and number is {second}".format(first=first, second=second))要么
(2) print('First number is', first, 'second number is', second)(注意:与逗号分开后,空格会自动添加)
要么
(3) print('First number %d and second number is %d' % (first, second))要么
(4) print('First number is ' + str(first) + ' second number is' + str(second))如果可能,最好使用
format() (1 /
1b)。



