【Moon晨的Python入门笔记】Learn Python The Hard Way(9 & 10 转义笔记 | 练习)

笔记

  • Python转义序列
转义字符功能
\\反斜杠( \ )
\'单引号( ' )
\"双引号( " )
\aASCII 响铃符(BEL)
\bASCII 退格符(BS)
\fASCII 进纸符(FF)
\nASCII 换行符(LF)
\N (name)Unicode 数据库中的字符名,其中 name 是它的名字,仅 Unicode 试用
\rASCII 回车符(CR)
\tASCII 水平制表符(TAB)
\uxxxx值为 16 位十六进制值 xxxx 的字符
\Uxxxxxxxx值为 32 位十六进制值 xxxxxxxx 的字符
\vASCII 垂直制表符(VT)
\ooo值为八进制 ooo 的字符
\xhh值为十六进制值 hh 的字符

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

练习

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
'''

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值