python字符串有哪些表达方式
主要是python中字符串的几种表达,表示方式。python的几种表达方式如下
1使用单引号扩起来字符串
>>>'mypythonlession'#以单引号将字符串扩起来
'mypythonlession'
>>>a='mypythonlession'
>>>print(a)
mypythonlession
2使用双引号将字符串扩起来
>>>"mypythonlession"#使用双引号将字符串扩起来
'mypythonlession'
>>>a="mypythonlession"
>>>print(a)
mypythonlession
3当想要输出单引号或者双引号时(将单引号,双引号作为普通字符输出),通过\进行转义
>>>'python\'escape'
"python'escape"
>>>a='python\'escape'
>>>print(a)
python'escape#通过\反斜线将单引号进行转移,不在乎最外层的是单引号还是双引号,反正是中间是字符串,
有\就将后面的单引号,双引号进行转义
>>>
>>>'python\"escape'
'python"escape'
>>>a='python\"escape'
>>>print(a)
python"escape
>>>
>>>"python\'escape"
"python'escape"
>>>a="python\'escape"
>>>print(a)
python'escape
>>>
>>>"python\"escape"
'python"escape'
>>>a="python\"escape"
>>>print(a)
python"escape
4通过单引号,双引号混合的方式输出单引号,双引号
>>>'"doublequote"'#单引号中,使用双引号,直接将双引号输出
'"doublequote"'
>>>a='"doublequote"'
>>>print(a)
"doublequote"
>>>
>>>"'singlequote'"#双引号中,使用单引号,将单引号输出
"'singlequote'"
>>>a="'singlequote'"
>>>print(a)
'singlequote'
>>>
>>>""double""#双引号中直接输出双引号报错
SyntaxError:invalidsyntax
>>>"\"double\""#双引号中直接输出双引号报错,但是将其中的双引号通过反斜线进行转义就可以了
'"double"'
>>>a="\"double\""
>>>print(a)
"double"
备注:在交互式解释器,输出的字符串都是通过引号扩起来的,如果输出有单引号,那么最终用双引号扩起来,如果输出字符串有双引号,用单引号扩起来,如果是单双混合,那么,用单引号扩起来,如果输出字符串没有单,双引号,用单引号扩起来。
以上内容为大家介绍了python培训之字符串有哪些表达方式,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






