4

Python__02--转义字符

 1 year ago
source link: https://blog.51cto.com/husheng/5936577
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Table of Contents

1 转义字符

定义:

转义字符(Escape character),所有的ASCII码都可以用“\”加数字(一般是8进制数字)来表示。而python中定义了一些字母前加""来表示常见的那些不能显示的ASCII字符,如\t,\n等,就称为转义字符,因为后面的字符,都不是它本来的ASCII字符意思了。

一句话:就是在一些字符前加 “\” 使它具有其他意义。

1.1 使用方式

  1. \n:换行

  2. \t:水平制表符Tab,1-4个空格

  3. \r:回车

    将光标回开头

    print(‘hello\rworld’)

    输出world会将hello覆盖

  4. \b:退格

  5. \:输出反斜杠

  6. ':输出单引号

  7. ":输出双引号

  8. 不希望字符串中的转义字符起作用,就使用原字符,在字符前加r或R

    注意:末尾不能是一个\,可以是两个

    print(r'hellow\nworld\\')
    

1.2 运行结果

1.2.1 完整代码

print('hellow\nworld')
print("==============1===============")
print('hellow\tworld')
print("==============2===============")
print('helloooo\tworld')
print("==============3===============")
print('hello\bworld')
print("==============4===============")
print(r'hellow\nworld\\')

1.2.2 运行结果

Python__02--转义字符_转义字符

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK