【问题标题】:Remove Space Between Text and Variables删除文本和变量之间的空格
【发布时间】:2021-03-09 20:45:26
【问题描述】:

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

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 年出生的。

【问题讨论】:

    标签: string spacing


    【解决方案1】:

    您可以在 Python 中使用“+”运算符来连接字符串,而无需添加空格。只需要注意string+int这种情况是不可能做到的,那么我们需要进行强制转换(conversion)才能按预期发生。

    还有其他方法可以在 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 + ".")
    

    【讨论】:

    • 做到了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 2015-07-30
    相关资源
    最近更新 更多