python中的转义字符

来源:互联网 发布:程序员赚外快平台 编辑:程序博客网 时间:2024/05/03 17:29

字符串是由‘’或者“”括起来的,如果所表示的字符串中含有引号,那怎么办呢?

1.如果字符串中包含“”,例如:

'learn "python" online '
  • 1

字符串外面用‘’括起来。

2.如果字符串中既包含‘ 又包含“ ,例如:

 he said "I'm hungry."
  • 1
  • 2
'he said \" I\'m hungry .\"'
  • 1

在 ’和“ 前面各插入一个转义字符\ 。 
注:转义字符\不计入字符串的内容。 
这里写图片描述

3.常用的转义字符:

EscapeWhat it does.含义 \\Backslash ()反斜杠\'Single-quote (')单引号\"Double-quote (")双引号\aASCII bell (BEL)响铃符\bASCII backspace (BS)退格符\fASCII formfeed (FF)进纸符\nASCII linefeed (LF)换行符\N{name}Character named name in the Unicode database (Unicode only)Unicode数据库中的字符名;name就是它的名字\r ASCIICarriage Return (CR)回车符\t ASCIIHorizontal Tab (TAB)水平制表符\uxxxxCharacter with 16-bit hex value xxxx (Unicode only)值为16位十六进制xxxx的字符\UxxxxxxxxCharacter with 32-bit hex value xxxxxxxx (Unicode only)值为32位十六进制xxxx的字符\vASCII vertical tab (VT)垂直制表符\oooCharacter with octal value ooo值为八进制ooo的字符\xhhCharacter with hex value hh值为十六进制数hh的字符