《笨办法学python3-Learn Python 3 the HARD WAY》-习题10 那是什么
程序员文章站
2022-07-01 17:19:28
...
学习内容:
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t*Catnip\n\t* Grass
"""
print (tabby_cat)
print (persian_cat)
print (backslash_cat)
print (fat_cat)
运行结果:
知识点:
- 转义序列
双反斜杠 \:打印出一个反斜杠
反斜杠 \ :转义序列
例:
①带反斜杠转义
print ("Hello, I'd said: \"finish the work now.\"") # 转义双引号
print ('Hello, I\'d said: "finish the work now."') # 转义单引号
②不带反斜杠转义:报错
print ("Hello, I'd said: "finish the work now."") # 无转义双引号
print ('Hello, I'd said: "finish the work now."') # 无转义单引号
2. 将转义序列和格式化字符组合到一起
my_name = "Zed"
my_age = "23"
my_eyes = "Black"
# 用到的转义符 \ 转义'就是语句中的I‘m
print (f'My name is {my_name}, I\'m {my_age} years old, and my eyes is {my_eyes}.')
# 用到的转义符 \\ 打印出一个\
print (f"My name is {my_name}, I'm a \\boy\\.")
# 用到的转义符 \t 用于打印(空格)的作用
print (f"\tI'm {my_age} years old.")
# 用到的转义符 \t 用于打印(空格)的作用 \n 换行
print (f"My eyes is {my_eyes},\n\tI'm chinese.")
3. 三个单引号(’’’ ‘’’)与三个双引号(""" “”")的选择
建议与字符串相结合,若字符串中包含单引号 (’),建议使用三个双引号(""" “”")。若是字符串中包含双引号("),建议使用三个单引号(’’’ ‘’’)。