Python basic function - print and escape character

1. print function

The print() method is used to print output, the most common function, the following is the common use of print
1. Output numbers

ptint(3+1)

2. Output string

print('hello world')

3. Expression of output operator

print(3+1)

4. Output data to a file
For example: automatically create a new test file and write text to the file

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

insert image description here
How to wrap the output, just add between characters

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

insert image description here

Two, escape character

Escape character, that is, \ + the first letter of the special function
1.\n: newline

print('hello\nworld')

Newline, ie n=newline
insert image description here
2.\t: tab character

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

insert image description here

3. \r: overwrite

print('hello\rworld')

Replace means to overwrite the previous things, and it is also understandable to select all hello and press a delete button
insert image description here
4.\b: delete

print('hello\bworld')

Equivalent to the bakspace button on the keyboard, delete the previous o
5. \: two backslashes represent a \

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

\ means a \, so add two \
insert image description here
6. The original character: make the escape character unaffected, write in front, use r or R

print(R'hello\nworld')

insert image description here
Note: There cannot be one \ at the end, but there can be two \

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

insert image description here

Guess you like

Origin blog.csdn.net/modi88/article/details/118281432