笔记
- Python转义序列
转义字符 | 功能 |
---|---|
\\ | 反斜杠( \ ) |
\' | 单引号( ' ) |
\" | 双引号( " ) |
\a | ASCII 响铃符(BEL) |
\b | ASCII 退格符(BS) |
\f | ASCII 进纸符(FF) |
\n | ASCII 换行符(LF) |
\N (name) | Unicode 数据库中的字符名,其中 name 是它的名字,仅 Unicode 试用 |
\r | ASCII 回车符(CR) |
\t | ASCII 水平制表符(TAB) |
\uxxxx | 值为 16 位十六进制值 xxxx 的字符 |
\Uxxxxxxxx | 值为 32 位十六进制值 xxxxxxxx 的字符 |
\v | ASCII 垂直制表符(VT) |
\ooo | 值为八进制 ooo 的字符 |
\xhh | 值为十六进制值 hh 的字符 |
练习
- ex9.py(具体语法规则详见:https://blog.youkuaiyun.com/weixin_40204888/article/details/83021196)
days = 'Mon Tue Wed Thu Fri Sat Sun'
# 赋值days以字符串:‘Mon Tue...Sun’
months = "Jan\nFed\nMar\nApr\nMay\nJun\nJul\nAug"
# 赋值months以字符串以星期,并且输出Jan后另取一行,再Fed,再另取一行,再Mar
print('Here are the days:',days)
# 输出字符串“Here...days”,然后输出 变量days(其对应的字符串)
print("Here are the months:",months)
# 输出字符串“Here...months”,然后输出 变量months
print('''There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6
''')
# 三引号内可随意折行,且会保持该格式输出。三引号内的 单/双引号都无需转义
'''
输出:
Here are the days: Mon Tue Wed Thu Fri Sat Sun
Here are the months: Jan
Fed
Mar
Apr
May
Jun
Jul
Aug
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6
'''
- ex10.py(主要练习转义符号)
persian_cat = "I'm split\non a line."
# 赋予左边变量右边的字符串。字符串先是正常的“I'm split”,然后换行后,接“on a line."
backslash_cat = "I'm \\ a \\ cat."
# 赋予左边变量右边的字符串。双斜杠最后是输出一个斜杠 \
fat_cat = """
I'll do a list:
\t* Fishies
\t* Catnip\n\t* Grass
"""
# 三双引,保存格式输出,其中\t是制表符,在开头,空8格,再输出文字
print(persian_cat)
print(backslash_cat)
print(fat_cat)
Shmily_means = "\n\tSee?\n\tHow much I love you"
print(f"I mean...{Shmily_means}")
x = "I mean...{}"
print("Long time no see...",x.format(Shmily_means))
'''
输出:
I'm split
on a line.
I'm \ a \ cat.
I'll do a list:
* Fishies
* Catnip
* Grass
I mean...
See?
How much I love you
Long time no see... I mean...
See?
How much I love you
'''