小白自學(xué)筆記之Python入門-第四章 常用模塊-string
4.4 string
Python 沒有字符數(shù)據(jù)類型,單個字符就是長度為 1 的字符串。string模塊主要包含關(guān)于字符串的處理函數(shù)。Python 中的字符串字面量由單引號或雙引號括起,'hello' 等同于 "hello",使用三引號"""或者'''可以將多行字符串賦值給變量。不要擔(dān)心一對單三引號之間是注釋,因為這里是賦值給變量哦!就像下面:
########################################
a="""1975年二、三月間,一個平平常常的日子,
細(xì)蒙蒙的雨絲夾著一星半點的雪花,正紛紛淋淋地向大地飄灑著。
時令已快到驚蟄,雪當(dāng)然再不會存留,往往還沒等落地,就已經(jīng)消失得無蹤無影了。
黃土高原嚴(yán)寒而漫長的冬天看來就要過去,但那真正溫暖的春天還遠(yuǎn)遠(yuǎn)地沒有到來。"""
?
b='''Finally , after walking about a hundred miles , we reached the gym . It’s this giant room with a basketball hoop at each end .
“Miss? Small?”? called? Miss? Daisy .? “Are you here?”
Nobody? answered ,? but? there? was? an echo? in? the? gym? so? we? could? hear? Miss Daisy’s? words? over? and? over? again? when they bounced off the walls .
“Miss? Small?? .? .? .? Miss? Small?? .? .? .? Miss Small? . . . Miss Small? . . . Are you here?. . . Are you here? . . . Are you here?”
It was cool .
“Hello!” I yelled .
The gym yelled back , “Hello! . . . Hello!. . . Hello! . . . Hello! . . . Hello!”
“Echo!” yelled Michael .
“Echo? .? .? .? echo? .? .? .? echo? .? .? .? echo , ”yelled the gym .
“A.J. is stupid!” yelled Ryan .
“A.J. is stupid! . . . A.J. is stupid! . . . A.J.
is? stupid!? .? .? .? A.J.? is? stupid!”? yelled? the gym .
I? was? gonna? yell ,? “Ryan? is? a? dumb-head , ”? but? instead? Miss? Daisy? yelled ,“Stop that , boys!”
“Stop that , boys! . . . Stop that , boys! . . .Stop that , boys!” yelled the gym .
It was cool .
'''
print(a)
print(b)
########################################
【上述文字資料漢語部分摘自《平凡的世界》開頭,英文部分摘自紐約暢銷書best seller Dan Gutman 的my weird school 系列(主要講的是美國低年級小學(xué)生的學(xué)校生活)之Miss small is off the wall ,by the way,我真的喜歡看】

Python 有一組可以在字符串上使用的內(nèi)建方法。而且所有字符串方法都返回新值,不會更改原始字符串,下表中列出了一些方法。



我不想寫了,太多了,有點印象就可以了,以后用到再查吧,下面看看幾個例子就ok了。
a = "Hello, 中國石油大學(xué)(北京)"
print(len(a))???????????????????????? #17
print(a.replace("Hello", "你好"))???? #你好, 中國石油大學(xué)(北京)
s=("信息學(xué)院","CISE")
print('**'.join(s))????????????????? #信息學(xué)院**CISE
name = "魏淑芬"
gender = "女"
age = 29
note = "至今未婚"
introduction = "俺叫{1},{0},今年{2}歲,{3}。"
print(introduction.format(gender, name, age,note))#俺叫魏淑芬,女,29歲,至今未婚
運(yùn)行結(jié)果如下:
17
你好, 中國石油大學(xué)(北京)
信息學(xué)院**CISE
俺叫魏淑芬,女,今年29歲,至今未婚。