我将如何接受用户的输入并在 Python shell 中重新打印它

How would I take a user's input and reprint it in the Python shell

我是 Python 的新手,我正在处理一些涉及名称的事情。例如:

example = raw_input ("What is your name?: ")

我将如何使用它让他们回答,并转载他们的名字,例如:

print "Hello, {name}!"
print "Hello, " + example + "!"
name = raw_input ("What is your name?: ")
print "Hello, " + name + "!"

你已经非常接近了,你已经有了正确的模板,现在你必须告诉它 {name} 应该替换成什么:

>>> example = "Foo"
>>> "Hello, {name}!".format(name=example)
'Hello, Foo!'

参见the docs for more information on using str.format

有多种方法可以实现您的目标。

如前所述,您可以使用:

name = raw_input ("What is your name?: ")
print "Hello, " + name + "!"

或者,

>>> example = "Foo"
>>> "Hello, {name}!".format(name=example)
'Hello, Foo!'

你也可以使用,

name = raw_input("What is your name?: ")
print "Hello, %s!" % name
print raw_input('Enter name :')

对于Python 3.5用户,它可以这样工作:

name = input("What is your name? ")  
print "Hello, %s!" % (name)

应该可以。