第七章預告:字符串、文本、各類打印、轉義序列、手動輸入raw_input()python
------------------------------------<習題6:字符串和文本>---------------------------------------app
在這裏還時有必要詳細介紹一下字符串以及和格式化字符之間的應用:ide
字符串:一般是指須要展現給別人的或者是想要從程序裏「導出」的一小段字符,吧啦吧啦吧啦吧啦,是否是以爲挺拗口的,不如舉個例子來的實在。函數
在ex1.py的這段代碼print "hello world!"中,雙引號括住的 hello world! 就是字符串。網站
格式化字符串的各類應用:參考ex6.py ui
1 # -*-coding:utf-8-*- 2 3 # 第一種:直接用%d 4 x = 'There are %d types of people' % 10 5 6 #第二種:用%s來將變量轉換成字符串 7 8 # 定義binary 9 binary = 'binary' 10 # 定義do_not 11 do_not = "don't" 12 # 定義y的意思(把變量轉換成字符串) 13 y = 'Those who know %s and those who %s.' % (binary, do_not) 14 15 # 輸出x 16 print x 17 # 輸出y 18 print y 19 20 #第三種:用%s和%r來將變量轉換成字符串,該變量也適用%s和%r來定義的 21 # 輸出 我說過了x (把變量轉換成字符串) 22 print 'I said: %r.' % x 23 # 輸出 我說過了y (把變量轉換成字符串) 24 print "I also said: '%s'." % y 25 26 # 定義hilarious 27 hilarious = False 28 # 用含有格式轉換符的字符串定義joke_evaluation 29 joke_evaluation = "Isn't that joke so funny?! %r" 30 31 # 輸出一個含有轉換符的變量 32 print joke_evaluation % hilarious 33 34 # 定義W和e 35 w = 'This is the left side of ...' 36 e = 'a string with a right side.' 37 38 # 輸出 w+e 39 print w+e
終端運行結果以下:this
求助!求助!求助!求助!:%r用來作調試比較好,由於它會顯示變量的原始數據(raw data),而%s和其它的轉換符號則是用來向用戶顯示輸出的!誰讀完這句話後可以不迷糊????lua
來個實例解釋一下,我尚未徹底理解。大概意思是%r會盡量的展現變量原始的模樣(函數、公式、字符串等等),而%s會將變量的最終結果展現出來,例如變量是一個調取日期的函數,%s直接能夠輸出結果,而%r會將函數也輸出出來。spa
見ex6_1.py代碼:<借鑑博客園用戶stellafan>命令行
1 #-*-coding:utf-8-*- 2 3 # %r和%s的區別 4 5 # 有些狀況下,二者處理的結果是同樣的,好比處理int型對象 6 print "1, I am %d years old" % 22 7 print "2, I am %s years old" % 22 8 print "3, I am %r years old" % 22 9 print "-"*20 10 11 12 # 不一樣狀況之一 13 text = "I am %d years old." % 22 14 print "4, I said: %s." % text 15 print "5, I said: %r." % text # %r打印輸出時可以重現它表明的對象,這裏輸出結果給字符串加上單引號 16 print "-"*20 17 18 #不一樣狀況之二 19 import datetime 20 d = datetime.date.today() 21 print "%s" % d 22 print "%r" % d 23 print "-"*20
終端運行結果以下:
------------------------------------<習題7&8&9:更多打印>---------------------------------------
這兩個習題只是展現了打印的幾種其它方式,我只貼上代碼,你們可自行研究:
1 print "Mary had a little lamb." 2 print "Its fleece was white as %s." % 'snow' 3 print "And everywhere that Mary went." 4 print "." * 10 # print 10 points 5 6 end1 = "c" 7 end2 = "h" 8 end3 = "e" 9 end4 = "e" 10 end5 = "s" 11 end6 = "e" 12 end7 = "B" 13 end8 = "u" 14 end9 = "r" 15 end10 = "g" 16 end11 = "e" 17 end12 = "r" 18 19 # watch that comma at the end. try removing it to see what happens 20 print end1 + end2 + end3 + end4 + end5 + end6, 21 print end7 + end8 + end9 + end10 + end11 + end12
1 # -*-coding:utf-8-*- 2 formatter = "%r %r %r %r" # 用"%r %r %r %r"試試,字符串中的格式轉換符之間不須要逗號 3 4 print formatter % (1, 2, 3, 4) #1,2,3,4 不須要用引號 5 print formatter % ("one", "two", "three", "four") # 必須用引號 6 print formatter % (True, False, False, True) #27習題中會介紹True和False的功能,不能用引號,由於引號使其變成字符串,而不是特殊意義的關鍵字 7 print formatter % (formatter, formatter, formatter, formatter) 8 print formatter %( 9 "I had this thing.", 10 "That you could type up right.", 11 "But it didn't sing.", 12 "So I said goodnight." 13 )
終端運行結果以下:
------------------------------------<習題10:轉義序列>---------------------------------------
這個習題中,總共列舉了15個轉義序列,可是總共就涉及到不到八個。下面是我在Evernote作的筆記:
有些人可能會問,對於換行符,響鈴符等有具體功能的轉義符號,還能理解,可是爲何會有 \\, \", \'呢? 其實我也不是特別理解,可是我遇到過一個狀況,就是須要輸入多個單引號時,python會誤覺得是字符串的標示符號。因此爲了讓python知道我僅僅是想輸出一個單引號,就用\'便可。
還有,\a真的會響的,代碼裏有一個會響一次,有兩個就會響兩次!參考ex10.py代碼。
ex10.py代碼以下:
1 # -*-coding:utf-8-*- 2 tabby_cat = "\tI'm tabbed in." 3 persian_cat = "I'm split/non a line." 4 backslash_cat = "I'm \\ a \\ cat ." 5 6 fat_cat = """ 7 \bI'll do a list: 8 \t* Cat food \a 9 \t* Fishies \a \a \a 10 \t* Catnip\n\t* Grass \a\ooo 11 """ 12 # \v垂直製表符,\t水平製表符 13 # \a是響鈴符,運行時會發出響鈴的聲音 14 # \b是退格符,運行時命令行會向前退一格 15 16 print tabby_cat 17 print persian_cat 18 print backslash_cat 19 print fat_cat
終端運行結果以下:
------------------------------------<習題11&12:raw_input()>---------------------------------------
講真的,這個習題在必定程度上調動了我繼續學下去的興趣。我相信你們天天都會在網站、App、遊戲等中,根據系統的提示,輸入一些信息,多是用戶名、密碼、電話號碼等等,raw_input()就是起到這個做用。當python運行到這個命令時,python會提醒你輸入一些信息。
鑑於這個習題的命令,須要本人輸入才能體會到,我就不在這裏插入終端運行結果了。
其實它很是的簡單,可是須要你親手運行一下ex11.py和ex12.py才能感覺獲得。代碼以下:
1 print "How old are you?", 2 age = raw_input() 3 print "How tall are you?", 4 height = raw_input() 5 print "How much do you weight?", 6 weight = raw_input() 7 8 print "so, you're %r old, %r tall and %r heavy." % ( age, height, weight) 9 10 11 # Differences between input() and raw_input() 12 print "How old are you?", 13 age = input() 14 print "How tall are you?", 15 height = input() 16 print "How much do you weight?", 17 weight = input() 18 19 print "so, you're %r old, %r tall and %r heavy." % ( age, height, weight)
raw_input()命令在運行時,咱們能夠在括號內寫一些提示,能夠提示用戶須要輸入哪些方面的信息。例如ex11_1.py
1 # print "What's her name?", 2 name = raw_input("name?") 3 print "How many years you have known her?", # 這個逗號,可讓你輸入的信息,緊接着這個問題,而不是換行。。 4 yars = raw_input("Years?") 5 print "Is she beautiful?", 6 answer = raw_input("yes or no?") 7 print "Do you like her or not?", 8 answer = raw_input("yes or no?")
raw_input()、int(raw_input())、input()的區別: 解釋見ex11_2.py代碼
1 #-*-coding:utf-8-*- 2 3 print 'x', 4 x = int(raw_input()) #只能輸入數字, 否者會報錯終止腳本運行 5 6 print 'y', 7 y = raw_input() #既能輸入數字,也能輸入字符 8 9 print 'z', 10 name = 'Neymagico' 11 z = input() #既能輸入數字,也能輸入字符, 可是字符被python認爲是個變量,須要對其定義 12 #若是輸入未定義的字符,會報錯
這一章就寫到這吧,由於下一章是關於參數、解包和變量相關的代碼,我本人也不是特別熟,想多用些篇幅再學一遍!
今天是父親節,我用raw_input()寫一段小代碼,表達一下對父親的感恩之情!
1 from sys import exit 2 3 4 print "Please write the words which you want to say to your father in his day!" 5 grate = [] 6 7 def respect(): 8 9 10 11 words = raw_input() 12 13 if words != 'stop': 14 15 grate.append(words) 16 print grate 17 respect() 18 19 else: 20 quit("ok, That's all to my father") 21 22 def quit(why): 23 print why 24 exit(0) 25 26 respect()
祝全天下的父親節日快樂!!!!!!!
第八章預告:參數、解包和變量、提示和傳遞、讀取文件、讀寫文件