如何在字符串上的第一个项目之前删除间距?

时间:2016-09-14 21:22:28

标签: python-3.x

示例:

time = "14:06"
Time = "2:06 PM"

print("The time you entered,",time,", is",Time,"in the 12-hour format.")

打印:您输入的时间 14:06,是12小时格式的下午2:06。

我希望14:06之后的空间不见了,所以它看起来像这样:

您输入的时间 14:06,是12小时格式的下午2:06。

2 个答案:

答案 0 :(得分:0)

print("The time you entered, {}, is {} in the 12-hour format.".format(time, Time))

https://docs.python.org/3.2/library/string.html#formatspec

PS:time是标准库的一个模块。我建议您改用time24time12

答案 1 :(得分:0)

sep=''添加到print语句中。这是print语句中所有项之间的自定义分隔符(在Python3中)。然后,您需要手动添加正确的间距。

time = "14:06"
Time = "2:06 PM"

# Original print statement
print("The time you entered,",time,", is",Time,"in the 12-hour format.", sep='')

# Updated print statement
print("The time you entered, ",time,", is ",Time," in the 12-hour format.", sep='')

<强>输出:

# Original
The time you entered,14:06, is2:06 PMin the 12-hour format.

# Updated
The time you entered, 14:06, is 2:06 PM in the 12-hour format.

https://docs.python.org/3/library/functions.html?highlight=print#print