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中的定義字符串一直回車,挨個循環輸出貌似就形成了一直轉的狀態,實際是挨個輸出形成的視覺錯誤(個人感覺)

 

運行結果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章