python基础函数-print及转义符

一、print函数

print() 方法用于打印输出,最常见的一个函数,以下为print常用用法
1.输出数字

ptint(3+1)

2.输出字符串

print('hello world')

3.输出运算符的表达式

print(3+1)

4.将数据输出到文件中
例如:自动新建一个test文件,向文件中写入文本

rw=open('D:/test.txt','a+') #a+表示文件不存在则创建,存在则在文件内追加文件内容
print('hello world',file=rw) #file=rw,指定文件
rw.close() #关闭文件

在这里插入图片描述
如何换行输出呢,字符之间新加 , 即可

print('hello','world','python')

在这里插入图片描述

二、转义符

转义符,即\ +特异功能的首字母
1.\n:换行

print('hello\nworld')

换行,即n=newline
在这里插入图片描述
2.\t:制表符

print('hello\tworld') #\t代表4个制表符号,是4个空格,但现在有3个空格,拆开hell,oXXX,world
print('helloooo\tworld') #中间是4个空格,拆开hell,oooo,xxxx,world

在这里插入图片描述

3.\r:覆盖

print('hello\rworld')

replace表示覆盖前面的东西,也可以理解把hello全选按了个删除按钮
在这里插入图片描述
4.\b:删除

print('hello\bworld')

相当于键盘上的bakspace按钮,将前一个o删除
5.\:两个反斜线表示一个\

print('heep:\\\\www.baidu.com')

\表示一个\,所以在加两个\
在这里插入图片描述
6.原字符:使转义字符不起左右,写在前面,用r或R

print(R'hello\nworld')

在这里插入图片描述
注意:末尾不能有一个\,但可以有两个\

#print(r'hello\world\')
print(r'hello\world\\')

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/modi88/article/details/118281432
今日推荐