python基础 字符串 利用三引号(''') 换行符 (\n)使打印的字符串进行换行 以及转义符(\)的使用

版权声明:伊凡ED原创 https://blog.csdn.net/Ivan_ED/article/details/81973822

python基础 字符串 利用三引号(”’) 换行符 (\n)使打印的字符串进行换行 以及转义符()的使用


利用三引号’‘’对打印的文字进行换行

例如:

1 print('''
2 xxxxxxxxx
3 xxxxxxxxx
4 xxxxxxxxx
5 xxxxxxxxx
6     ''')

xxxxxxxxx
xxxxxxxxx
xxxxxxxxx
xxxxxxxxx

进程已结束,退出代码      

利用换行符对打印的字符串进行换行

并且通常换行最常用的方法也是使用换行符 \n 进行换行
例如:

1 print'hello''\nworld')

hello
world

进程已结束,退出代码

转义符 \

虽然换行符可以做到换行,但有时也会出现特殊情况,比如一下的例子:

print('D:\note')

D:
ote

进程已结束,退出代码

如果我们有我们的转义符 将能轻松的输入你想要的字符
例如:

print('D:\\note')

D:\note

进程已结束,退出代码

转义符还可以将很长的代码链接在一起 不会被换行打断
例如:

print("\
xxxxxxxxx\
xxxxxxxxx\
xxxxxxxxx\
xxxxxxxxx\
   ")

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  

进程已结束,退出代码

使用转义符需要注意在字符串最后一位不能使用转义符 \ ,如果使用python将无法判断字符串的结束为止 如果必须用 \ 结尾 \ 必须用\进行转义:

print('hello world\\')


hello world\

进程已结束,退出代码

猜你喜欢

转载自blog.csdn.net/Ivan_ED/article/details/81973822
今日推荐