转义字符

要输出:

"Life is short,
let's learn Python"

代码:

1 print('\"Life is short,\n let\'s learn Python\"')

 运行结果:

1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a1.py
2 "Life is short,
3  let's learn Python"
4 
5 Process finished with exit code 0

 

输出路径:

代码

1 # 方法一:使用转义字符\\表示\
2 print('D:\\Program Files\\MongoDB\\Server')
3 # 方法二:使用原始字符串,在字符串的前面加r,
4 # 这样转义字符将不再有效,每一个字符将不再有特殊的含义
5 print(r'D:\Program Files\MongoDB\Server')

运行结果:

"D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a1.py
D:\Program Files\MongoDB\Server
D:\Program Files\MongoDB\Server

Process finished with exit code 0

 

不用转义字符,使用三引号实现换行

代码:

1 a = '''
2 你好啊
3 你吃饭没有,
4 你是哪里人?
5 '''
6 print(a)

运行结果:

1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a2.py
2 
3 你好啊
4 你吃饭没有,
5 你是哪里人?
6 
7 
8 Process finished with exit code 0

 

 

常用转义字符

 

posted @ 2020-07-07 23:21  youngxinwei  阅读(92)  评论(0编辑  收藏  举报