日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python入門-字符串初相識(shí)

 本文轉(zhuǎn)載自微信公眾號(hào)「尤而小屋」,作者Peter。轉(zhuǎn)載本文請(qǐng)聯(lián)系尤而小屋公眾號(hào)。

大家好,我是Peter呀~

從本文開始準(zhǔn)備介紹Python中的常見數(shù)據(jù)結(jié)構(gòu):字符串、列表、集合、字典。其中字符串、列表、字典應(yīng)用非常頻繁,需要重點(diǎn)掌握,本文介紹的是字符串及相關(guān)操作和方法。最后的字符串3種格式化方法將在下篇文章詳細(xì)講解。

一、認(rèn)識(shí)字符串

字符串在Python中是一種數(shù)據(jù)對(duì)象類型,用str表示,通常用單引號(hào)或者雙引號(hào)包裹起來(英文的半角符號(hào))

字符串string,是有零個(gè)或者多個(gè)字符組成的有限串行,通常記為s=a[1]a[2]...a[m]

 
 
 
 
  1. strings = "hello world"  # 雙引號(hào) 
  2. strings 

'hello world'

 
 
 
 
  1. type(strings) 

str

 
 
 
 
  1. new_strings = 'hello python'  # 單引號(hào) 
  2. new_strings 

通過type函數(shù)查看類型

 
 
 
 
  1. type(new_strings)   

str

 
 
 
 
  1. type(100)  # 數(shù)值型 

int

 
 
 
 
  1. type("100")  # 字符串類型 

str

如果字符串本身內(nèi)容就有引號(hào),我們有兩種解決方式:

  • 雙引號(hào)包裹單引號(hào)
  • 使用轉(zhuǎn)義字符
 
 
 
 
  1. # 如果字符串本身內(nèi)容也包含引號(hào) 
  2. # 1、雙引號(hào)包裹單引號(hào) 
  3.  
  4. x = "I'm Peter!" 

"I'm Peter!"

 
 
 
 
  1. # 使用轉(zhuǎn)義字符\ 
  2.  
  3. y = 'I\'m Peter' 

"I'm Peter!"

 
 
 
 
  1. # 3、使用r“字符內(nèi)容":原始字符串 
  2.  
  3. z = r"I'm Peter!" 

二、字符串基礎(chǔ)操作

2.1鍵盤輸入

鍵盤輸入的任何內(nèi)容都是字符串

 
 
 
 
  1. name = input("my name is: ") 

my name is: Peter

 
 
 
 
  1. name  # 返回的是字符串類型數(shù)據(jù) 

'Peter'

 
 
 
 
  1. # 鍵盤輸入的都是字符串類型數(shù)據(jù) 
  2.  
  3. age = input("my age is: ")   

my age is: 20

 
 
 
 
  1. type(age)  # 返回的仍然是字符串 

str

2.2變量與字符串

python中有這樣一句話:變量是無類型的,對(duì)象有類型

在下面的列子中,我們看到:變量x既可以是int類型,也可以是字符類型;但是數(shù)值5和字符串python都是有自己固定的數(shù)據(jù)類型。

 
 
 
 
  1. x = 5  # 變量x可以貼在int類型的數(shù)字5上:賦值語句 
 
 
 
 
  1. x  = "python"  # 變量x也可以貼在字符串類型上 
 
 
 
 
  1. # 通過賦值語句來表示變量和字符串對(duì)象之間的引用關(guān)系 
  2.  
  3. a = "hello-python" 

'hello-python'

 
 
 
 
  1. type(a) 

str

2.3查看字符串地址

 
 
 
 
  1. id(a) 

4516524144

 
 
 
 
  1. id(age) 

4516499824

2.4原始字符串

用r開頭引起的字符串就是我們常用的原始字符串,放在里面的任何字符串都是表示它的原始含義,從此不需要轉(zhuǎn)義

 
 
 
 
  1. s = "hello \npython" 
  2. print(s)  # 發(fā)生換行 

hello

python

 
 
 
 
  1. # 如何解決:1-使用轉(zhuǎn)義字符 
  2. print("hello \\npython") 

hello \npython

 
 
 
 
  1. # 2-使用r包裹起來 
  2. print(r"hello \npython") 

hello \npython

三、索引和切片

索引和切片是python中非常重要的一個(gè)概念,記住幾點(diǎn):

  • 索引左邊從0開始,右邊從-1開始
  • 切片語法:start:end:step,step表示步長(zhǎng)

3.1索引

使用的index()來查看某個(gè)字符的索引

 
 
 
 
  1. str1 = "python" 
  2. id(str1) 

4473172336

 
 
 
 
  1. str2 = "thonpy" 
  2. id(str2) 

4516506736

 
 
 
 
  1. # 尋找某個(gè)字符的索引index:索引從0開始 
  2.  
  3. str1.index("h") 

3

 
 
 
 
  1. str1.index("n") 

5

3.2切片

關(guān)于切片總結(jié)4點(diǎn):

  • 標(biāo)準(zhǔn)形式:start:stop:step
  • 含頭不含尾:包含start部分,不包含stop部分
  • 切片的時(shí)候,索引左邊從0開始,右邊從-1開始
  • 步長(zhǎng)step可正可負(fù)
 
 
 
 
  1. str3 = "learn python" 
  2. str3 

'learn python'

 
 
 
 
  1. # 標(biāo)準(zhǔn)切割 
  2.  
  3. str3[0:4:1] # 步長(zhǎng)為1 

'lear'

 
 
 
 
  1. str3[:4:1] # 開頭的0可以省略 

'lear'

 
 
 
 
  1. str3[:4]  # 步長(zhǎng)1也可以省略 

'lear'

 
 
 
 
  1. str3[0:4:2] # 步長(zhǎng)為2 

'la'

 
 
 
 
  1. str3[:10]  # 步長(zhǎng)為1,切到索引為10,不包含10 

'learn pyth'

 
 
 
 
  1. str3[10:0:-2] # 步長(zhǎng)為2 

'otpna'

 
 
 
 
  1. str3.index("o")  # 從索引10的o字符開始切割,往前切 

10

四、字符串進(jìn)階操作

4.1求長(zhǎng)度

 
 
 
 
  1. len(str3) 

12

4.2返回最值

每個(gè)字符都有自己對(duì)應(yīng)的數(shù)字編碼,通過比較數(shù)字就可以知道對(duì)應(yīng)字符的大小

 
 
 
 
  1. max(str3)  # 根據(jù)ASCII碼的取值來決定 

'y'

 
 
 
 
  1. min(str3) 

' '

 
 
 
 
  1. ord("y")  # 每個(gè)字符對(duì)應(yīng)的編碼 

121

 
 
 
 
  1. ord("z") 

122

 
 
 
 
  1. ord(" ")   

32

 
 
 
 
  1. chr(121)   # 數(shù)值對(duì)應(yīng)的字符:反編碼的過程 

'y'

 
 
 
 
  1. "aa" > "ab"  # 第一個(gè)字符相同就比較第二個(gè) 

False

 
 
 
 
  1. "aac" > "aab"  # c 大于 b 

True

4.3判斷是否存在

 
 
 
 
  1. "p" in str3 

True

 
 
 
 
  1. "q" in str3 

False

 
 
 
 
  1. str3 

'learn python'

4.4字符串重復(fù)

 
 
 
 
  1. str1 

'python'

 
 
 
 
  1. str1 * 3 

'pythonpythonpython'

4.5字符串連接

兩種方式:

  • 通過+來實(shí)現(xiàn)
  • 通過join來實(shí)現(xiàn)
 
 
 
 
  1. str1 * 3 

'python'

 
 
 
 
  1. str4 = "learn "  # 后面有個(gè)空格 
  2. str4 

'learn '

 
 
 
 
  1. str4 + str1 

'learn python'

 
 
 
 
  1. "I" + " " + "am" + " Peter"  # 使用+號(hào)多次連接 

'I am Peter'

 
 
 
 
  1. # join連接 
  2.  
  3. " ".join(("learn","python"))  # 連接符號(hào)為空格 

'learn python'

 
 
 
 
  1. "+".join(("learn","python"))  # 連接符號(hào)為+ 

'learn+python'

 
 
 
 
  1. " ".join(("I","am", "Peter"))   

'I am Peter'

 
 
 
 
  1. 8 + "python"   # 不同類型的數(shù)據(jù)不能相加,看下面的報(bào)錯(cuò) 

---------------------------------------------------------------------------

 
 
 
 
  1. TypeError                                 Traceback (most recent call last) 
  2.  
  3.  in  
  4. ----> 1 8 + "python"   # 不同類型的數(shù)據(jù)不能相加 

TypeError Traceback (most recent call last) in  ----> 1 8 + "python" # 不同類型的數(shù)據(jù)不能相加TypeError: unsupported operand type(s) for +: 'int' and 'str'

 
 
 
 
  1. "8" + "python" 

'8python'

 
 
 
 
  1. str(8) + "python"  # 使用str函數(shù)強(qiáng)制轉(zhuǎn)換 

'8python'

五、常用字符串方法

5.1判讀是否全部為字母

 
 
 
 
  1. "python".isalpha() 

True

 
 
 
 
  1. "8python".isalpha() 

False

5.2分割字符串

 
 
 
 
  1. str5 = "My name is Peter" 
  2. str5.split(" ")  # 通過空格進(jìn)行分割,得到的是列表(后面會(huì)介紹列表) 

['My', 'name', 'is', 'Peter']

 
 
 
 
  1. str5.split()   # 默認(rèn)是空格切割,效果同上 

['My', 'name', 'is', 'Peter']

 
 
 
 
  1. str5.split("")  # 報(bào)錯(cuò)空切割字符 

---------------------------------------------------------------------------

 
 
 
 
  1. ValueError                                Traceback (most recent call last) 
  2.  
  3.  in  
  4. ----> 1 str5.split("")  # 報(bào)錯(cuò)空切割字符 

ValueError: empty separator

 
 
 
 
  1. str5.split("is")   # 通過is來切割 

['My name ', ' Peter']

5.3去掉字符串的空格

  • strip():兩端的空格
  • lstrip():左邊的空格
  • rstrip():右邊的空格
 
 
 
 
  1. str6 = " python "  # 左右各一個(gè)空格 
  2. str6 

' python '

 
 
 
 
  1. str6.strip() 

'python'

 
 
 
 
  1. str6.rstrip() 

' python'

 
 
 
 
  1. str6.lstrip() 

'python '

 
 
 
 
  1. str6   # 原來的值保持不變 

' python '

5.4字符大小寫轉(zhuǎn)化

python中實(shí)現(xiàn)各種類型的大小寫轉(zhuǎn)化

  • upper():字母全部轉(zhuǎn)為大寫
  • lower():字母全部轉(zhuǎn)為小寫
  • capitalize():首字母全部轉(zhuǎn)為大寫
  • title():字符串中所有單詞的首字母大寫,其他為小寫
  • isupper():判斷字母是否全部轉(zhuǎn)為大寫
  • islower():判斷字母是否全部轉(zhuǎn)為小寫
  • istitle():判斷是否為標(biāo)題模式,即字符串中所有單詞的首字母大寫,其他為小寫
 
 
 
 
  1. str7 = "this is Python"  # 只有P是大寫 
  2. str7 

'this is Python'

 
 
 
 
  1. str7.upper()  # 全部為大寫 

'THIS IS PYTHON'

 
 
 
 
  1. str7.lower()  # p也變成了小寫 

'this is python'

 
 
 
 
  1. str7.capitalize()  # 首字母T大寫 

'This is python'

 
 
 
 
  1. str7.islower()  # 是否全部為小寫 

False

 
 
 
 
  1. str7.isupper()  # 是否全部為大寫 

False

 
 
 
 
  1. str7.istitle()  # 是否為標(biāo)題模式 

False

 
 
 
 
  1. str7.title() # 轉(zhuǎn)成標(biāo)題模式:每個(gè)單詞的首字母大寫 

'This Is Python'

總結(jié)

字符串在Python中是非常高頻使用的是一種數(shù)據(jù)類型,從字符串的轉(zhuǎn)化、獲取字符串指定中的指定內(nèi)容、字符串的切片索引等都是必須掌握的知識(shí)點(diǎn),希望本文對(duì)讀者有所幫助!


網(wǎng)頁題目:Python入門-字符串初相識(shí)
標(biāo)題來源:http://www.5511xx.com/article/cdejecj.html