繁体   English   中英

单引号、双引号作为 Python 输入值

[英]Single quotes, Double quotes as Python input values

我使用了以下命令,每次都在变量中存储了不同的值。

name=input('enter your name:')
enter your name:'' (two single quotes)
name
"''" (this is how it is displayed after I type 'Name' and press 'Enter')

name=input('enter your name:')
enter your name:"" (two double quotes)
name
'""' (this is how it is displayed after I type 'Name' and press 'Enter')

name=input('enter your name:')
enter your name:"'"' (double, single, double, single)
name
'"\'"\'' (this is how it is displayed after I type 'Name' and press 'Enter')

name=input('enter your name:')
enter your name:'"'" (single, double, single, double)
name
'\'"\'"' (this is how it is displayed after I type 'Name' and press 'Enter')

\\ 在变量“名称”中表示什么,为什么会出现在那里?

它用于在单引号字符串中转义单引号。 这种“转义”允许您在字符串中使用单引号和/或双引号。 您可以通过在用单引号引用的字符串中使用双引号来解决此问题,反之亦然。 但是,如果您有一个带单引号和双引号的字符串,这将不再起作用。 所以你现在使用转义。

Python 控制台只是想让您看到变量,而不是变量的内容,这就是您看到转义的原因。 您也可以只显示带有 print 的字符串,而您不会看到转义,因为您现在只能按原样看到字符串。

所以,只需使用print(name)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM