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

时间:2018-09-29 20:50:32

标签: python python-3.x

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

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

\在变量“名称”中表示什么?为什么在变量中根本没有?

1 个答案:

答案 0 :(得分:1)

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

Python控制台只希望您查看变量,而不是变量内容,这就是为什么您要转义。您还可以只显示带有print的字符串,而不会看到转义,因为现在您只看到了字符串。

因此,只需使用print(name)