Python入门系列(五)

Python如同C语言,C++。同样支持转义符,那我们看看Python中都有哪些转义符呢?

1、\\ 反斜杠

>>> days="\\Mon\\Tue\\Wed\\Thu\\Fir\\Sat\\Sun"
>>> print days
\Mon\Tue\Wed\Thu\Fir\Sat\Sun

2、\' 单引号

>>> days="\'Mon\',\'Tue\',\'Wed\',\'Thu\',\'Fir\',\'Sat\',\'Sun\'"
>>> print days
'Mon','Tue','Wed','Thu','Fir','Sat','Sun'

3、\" 双引号

>>> days="\"Mon\",\"Tue\",\"Wed\",\"Thu\",\"Fir\",\"Sat\",\"Sun\""
>>> print days
"Mon","Tue","Wed","Thu","Fir","Sat","Sun"

 4、\t 制表符

>>> days="Mon\tTue\tWed\tThu\tFir\tSat\tSun\t"
>>> print days
Mon     Tue     Wed     Thu     Fir     Sat     Sun

5、\a 响铃

 

>>> days="Mon\aTue\aWed\aThu\aFir\aSat\aSun\a"
>>> print days
MonTueWedThuFirSatSun

6、\b 退格

>>> days="Mon\bTue\bWed\bThu\bFir\bSat\bSun\b"
>>> print days
MoTuWeThFiSaSun
>>> days="Mon\b"
>>> print days
Mon

请注意,退格键在最后一个字符的处理

7、\000 空​​​​​​​

>>> days="Mon\000Tue\000Wed\000\Thu\000Fir\000Sat\000Sun\000"
>>> print days
Mon

8、\n 换行

>>> days="Mon\nTue\nWed\nThu\nFir\nSat\nSun\n"
>>> print days
Mon
Tue
Wed
Thu
Fir
Sat
Sun

9、\v 纵向制表符

>>> print "Mon",'\v',"Tue",'\v',"Wed",'\v',"Thu",'\v',"Fir",'\v',"Sat",'\v',"Sun"
Mon 
    Tue 
        Wed
            Thu
                Fir
                    Sat
                        Sun

10、\t 横向制表符 

>>> days="Mon\tTue\tWed\tThu\tFir\tSat\tSun\t"
>>> print days
Mon     Tue     Wed     Thu     Fir     Sat     Sun

11、\r 回车

>>> days="Mon\rTue\rWed\rThu\rFir\rSat\rSun\r"
>>> print days
Sun
>>> days="Mon\r"
>>> print days
Mon

大家可以思考一下,为什么第一个只能打印出Sun

12、\f​​​​​​​ 换页​​​​​​​

>>> days="Mon\fTue\fWed\fThu\fFir\fSat\fSun\f"
>>> print days
Mon 
    Tue 
        Wed
            Thu
                Fir
                    Sat
                        Sun

 

你可能感兴趣的:(Python,Python学习)