首页 > 其他 > 详细

转义字符

时间:2020-07-07 23:51:29      阅读:104      评论:0      收藏:0      [点我收藏+]

要输出:

"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  lets learn Python"
4 
5 Process finished with exit code 0

 

输出路径:

代码

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

 

 

常用转义字符

技术分享图片

 

转义字符

原文:https://www.cnblogs.com/youngxinwei/p/13264203.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!