好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

【Python从零到壹】转义字符与原字符

概念: 转义字符: 表示有特殊含义的字符,如\n表示换行,是字母newline的首字母 \t表示 制表符

print(‘hello\nworld’) #\ +转义功能的首字母 这里的表示newline的首字符:表示换行

print(‘hello\tworld’) #\t表示制表位

print(‘hello\rworld’) #world 将hello进行了复制

print(‘hello\bworld’) #\b是退一个格,将o退没了 print(‘http:\\HdhCmsTestgeekyunwei测试数据’) print(‘老师说:‘大家好’’)

原字符的概念:

#原字符: 不希望字符串中的转义字符起作用,就是用原字符,就是在字符串前面加上r或者R

print(r’hello\nworld’)

#注意在最后不能是\可以用\代替

输出结果:

E:\Python_demo\vippython\venv\Scripts\python.exe?E:/Python_demo/vippython/day02.py
hello
world
hello	world
world
hellworld
http:\\HdhCmsTestgeekyunwei测试数据
老师说:'大家好'hello\nworld

测试一下后面加\的时候:

#?作者:互联网老辛#?开发时间:2021/4/2/0002?20:24print(r'hello\nworld\')

报错信息:

E:\Python_demo\vippython\venv\Scripts\python.exe?E:/Python_demo/vippython/test.py
??File?"E:/Python_demo/vippython/test.py",?line?4
????print(r'hello\nworld\')??^
SyntaxError:?EOL?while?scanning?string?literal

Process?finished?with?exit?code?1

所以如果你要在后面加\一定是\

#?作者:互联网老辛#?开发时间:2021/4/2/0002?20:24print(r'hello\nworld\\')

输出结果:

查看更多关于【Python从零到壹】转义字符与原字符的详细内容...

  阅读:38次