如何在使用多部分字符串时获得输入?

时间:2017-11-30 15:55:38

标签: python

我有一些代码,类似于

name = input("What is your name?")
press_enter_to_continue = input("Well", name, ", you've got a long journey ahead of you.")

我最终得到了多余参数的错误,所以我怎么能重做代码呢?

4 个答案:

答案 0 :(得分:1)

您可以使用字符串格式:

name = input("What is your name?")
press_enter_to_continue = input("Well {} you've got a long journey ahead of you.".format(name))

答案 1 :(得分:1)

将变量添加到字符串有几种不同的方法:

input("Well {}, you've got a long journey ahead of you.".format(name))

如果你使用的是python 3.6,你可以这样做:

input(f"Well {name}, you've got a long journey ahead of you.")

或者如果你想让它保持超级简单:

input("Well " + name + ", you've got a long journey ahead of you.")

答案 2 :(得分:0)

作为使用字符串格式的替代方法,您可以使用+运算符来连接字符串。

name = input("What is your name?")
press_enter_to_continue = input("Well" + name + ", you've got a long journey ahead of you.")

答案 3 :(得分:0)

您可以使用+连接字符串,尽管Ajax1234的答案是更好的选择:

name = input("What is your name?")
press_enter_to_continue = input("Well" + name + ", you've got a long journey ahead of you.")