python - 单引号,双引号作为 Python 输入值
问题描述
我使用了以下命令,每次都将不同的值存储在变量中。
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)
推荐阅读
- hibernate - 如何在 SELECT 查询中制作休眠前缀 N?
- asp.net-mvc - 用控件填写表单(根据服务返回的数据)
- excel - 创建具有多个工作条件的 IF 语句
- ios - 如何用渐变颜色的控制点填充贝塞尔路径
- python - Python - Intcheck 函数不起作用
- windows - PNRP 停止工作窗口 10 1803
- typescript - 在 Typescript 中将函数 keyEvent 更改为 swipeEvent
- javascript - localstorage (js) 中的购物卡值
- google-sheets - 受 Google 电子表格保护的 Web 应用访问级别 任何人
- javascript - 遍历对象数组并创建一个新对象