python 笔记 转义序列与简单无限循环——12.24
习题9:打印,打印,打印
话不多说,先熟悉下手感
ex9.py
# -*- coding: utf-8-*-
# Here's some new strange stuff,remember type it exactly.
days ="Mon Tue Wed Thu Fri SatSun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print "Here are the days: ", days
print "Here are the months: ", months
print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as welike.
Even 4 lines if we want, or 5, or 6.
"""
运行结果:
感悟与自我测试:
学习到了2个知识点:
1:\n表示换行
2:打印一段话,前后均用"""符号包裹住就好。
ex9_1.py
#-*- coding: utf-8-*-
name ="Jack Mary Kitty BlackTom"
hobbies = "singingg\nruningg\nclimbing"
print "I have some good friends.\nThey are" +name
print "In the weekend, I will do some thing:",hobbies
print "Inthe weekend, I will do some thing:"+hobbies #此处对比,+好和逗号看起来区别不大,本质上讲+表连接,逗号表分隔参数
运行结果:
习题 10: 那是什么?
上一个练习就是两种让字符串扩展到多行的方法:使用\n和转义符(转义序列)
ex10.py
# -*- coding: utf-8-*-
tabby_cat = "\tI'm tabbledin."
persian_cat = "I'm split\non aline."
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
运行结果:
转义符 |
功能 |
\\ |
Backslash() 反斜杠 |
\' |
Single quote (‘) 单引号 |
\" |
Double quote (”) 双引号 |
\a |
ASCII Bell (BEL) 响铃符 |
\b |
ASCII Backspace (BS) 退格符 |
\f |
ASCII Formfeed (FF) 进纸符 |
\n |
ASCII Linefeed (LF) 换行符 |
\N{name} |
Unicode 数据 库中的字符名, 其中 name 就 是它的名字 (Unicode only) |
\r ASCII |
Carriage Return (CR) 回车符 |
\t ASCII |
Horizontal Tab (TAB) 水 平制表符 |
\uxxxx |
值为 16 位十 六进制值 xxxx 的字符 (Unicode only) |
\Uxxxxxxxx |
值为 32 位十 六进制值 xxxx 的字符 (Unicode |
\v |
ASCII Vertical Tab (VT) 垂直制 表符 |
\ooo |
为八进制值 ooo 的字符 |
\xhh |
值为十六进制 数 hh 的字符 |
ex10_1.py
# -*- coding: utf-8-*-
tabby_cat = "\tI'm tabbled 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
while True:
for i in["/","-","|","\\","|"]:
print "%s \r" % i,
#结果很好玩,出现一个| 一直转,貌似是一个简单的无限循环,将I中的定义字符串一直回车,挨个循环输出貌似就形成了一直转的状态,实际是挨个输出形成的视觉错误(个人感觉)
运行结果: