简体   繁体   English

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

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

I used the following command and I got different values stored in the variable each time.我使用了以下命令,每次都在变量中存储了不同的值。

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')

What does the \\ indicate in the variable 'name' and why is it at all there? \\ 在变量“名称”中表示什么,为什么会出现在那里?

It is there to escape a single quote in a single quotes strings.它用于在单引号字符串中转义单引号。 This "escaping" allows you to use single and/or double quotes in a string.这种“转义”允许您在字符串中使用单引号和/或双引号。 You can work around this by using a double quote in a string quoted with single quotes are vice versa.您可以通过在用单引号引用的字符串中使用双引号来解决此问题,反之亦然。 But this does not work anymore if you have a string with single quotes and double quotes.但是,如果您有一个带单引号和双引号的字符串,这将不再起作用。 So you have use escaping now.所以你现在使用转义。

The Python console just wants you to see the variable, not the contents of the variable that's why you see escaping. Python 控制台只是想让您看到变量,而不是变量的内容,这就是您看到转义的原因。 You can also just display the string with print and you won't see the escaping, because you now just see the string as is.您也可以只显示带有 print 的字符串,而您不会看到转义,因为您现在只能按原样看到字符串。

So, just use print(name)所以,只需使用print(name)

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

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