python – print()在结果中显示引号

当脚本的下面部分被激活时,它会在结果中显示所有逗号和单引号(和括号).

print(name, 'has been alive for', days, 'days', minutes, 'minutes and', seconds, 'seconds!')

所以,例如:

('Ryan', 'has been alive for', 10220, 'days', 14726544, 'minutes and', 883593928, seconds!')

我想清理它,所以看起来不错.那可能吗?

“好”,我的意思是这样的:

Ryan has been alive for 10220 days, 14726544 minutes, and 883593928 seconds!

这是完整的脚本:

print("Let's see how long you have lived in days, minutes, and seconds!")
name = raw_input("name: ")

print("now enter your age")
age = int(input("age: "))

days = age * 365
minutes = age * 525948
seconds = age * 31556926

print(name, 'has been alive for', days, 'days', minutes, 'minutes and', seconds, 'seconds!')

raw_input("Hit 'Enter' to exit!: ")

最佳答案 你需要从__future__ import print_function.

在Python 2.x中,当您使用3.x语法时,您正在执行的操作被解释为打印元组.

例:

>>> name = 'Ryan'
>>> days = 3
>>> print(name, 'has been alive for', days, 'days.')
('Ryan', 'has been alive for', 3, 'days.')
>>> from __future__ import print_function
>>> print(name, 'has been alive for', days, 'days.', sep=', ')
Ryan, has been alive for, 3, days.
点赞