为什么Hello David 之间有一个空格?

问题描述 投票:0回答:1
name = input("What is your name? ")
name = name.strip()
print(f"Hello, {name}")

这个代码来自 CS50 的 python 课程,我没有得到它的输出是如何的

Hello(space)David
作为示例。怎么还有空间?

附注我想这是个初学者问题,但我是初学者所以:)

python cs50 space
1个回答
0
投票

你被迫呆在那里的空间就在这里:

print(f"Hello, {name}")
              ^
              |
       this adds the space

f 字符串(

f"string"
格式字符串)表示打印
"Hello, "
(带空格),后跟
{name}
,这是变量
name
直接替换到该位置的 f 字符串中。

© www.soinside.com 2019 - 2024. All rights reserved.