name = input("What is your name? ")
name = name.strip()
print(f"Hello, {name}")
这个代码来自 CS50 的 python 课程,我没有得到它的输出是如何的
Hello(space)David
作为示例。怎么还有空间?
附注我想这是个初学者问题,但我是初学者所以:)
你被迫呆在那里的空间就在这里:
print(f"Hello, {name}")
^
|
this adds the space
f 字符串(
f"string"
格式字符串)表示打印 "Hello, "
(带空格),后跟 {name}
,这是变量 name
直接替换到该位置的 f 字符串中。