删除文本和变量之间的 Space

Remove Space Between Text and Variables

这就是我目前所拥有的 - 但我无法摆脱感叹号和句点之前的空格。我错过了什么?

user_name = input("What is your name? ")
user_age = int(input("How old are you? "))

year_born = 2021 - user_age

print("Hello", user_name, "!", "You were born in", year_born, ".")

我的输出 - 你好阿曼达!你是2005年出生的。

您可以在 Python 中使用“+”运算符连接字符串,而无需添加空格。请注意,在这种情况下不可能执行 string + int,然后我们需要按预期进行转换(转换)。

在 Python 中还有其他方法可以比我记得的更简单地连接字符串。

您的代码会很简单:

user_name = input("What is your name? ")
user_age = int(input("How old are you? "))

year_born = str(2021 - user_age)

print("Hello", user_name + "!", "You were born in", year_born + ".")