Python中的转义字符和结束符的应用与理解

2022-03-03  本文已影响0人  可可里西

一、转义字符

转义字符有很多,这里我就只讲解连个转移字符,分别是换行符和制表符。

注意:\叫做反斜杠,/叫做斜杠

# \n:换行
# 需求: 让PYthon自学网每个词都换行

# 1.老方法
print('Python')
print('自')
print('学')
print('网')
# 返回结果
Python
自
学
网

#  2.利用换行转义字符 反斜杠n:\n
print('Python\n自\n学\n网')
# 返回结果
Python
自
学
网

# \t:制表符
#  需求: PYthon自学网首行缩进一个tab键
print('\tPYthon自学网')
# 返回结果
   PYthon自学网

二、结束符

Print()函数的结束符也是为了格式化数据用的,其实确切的说,如果设置了print函数的结束符号我们可以控制格式化数据的不同展示方式。

问题: 想一想为什么两个print会换行输出
print('输出的内容',end="\n")

在Python中,print()函数默认自带end=”\n”这个换行结束符,所以导致每2个print直接会换行展示,用户可以按需求更改结束符

#  默认的转义字符\n
print('hello')
print('Python')
#  返回结果
hello
Python

# 换成转义字符\t ----一个tab键
print('hello',end="\t")
print('Python')
#返回结果
hello  Python

# 换成其他符号 ****
print('hello',end="****")
print('Python')
# 返回结果
hello****Python
上一篇 下一篇

猜你喜欢

热点阅读