Python入門-字符串初相識
本文轉(zhuǎn)載自微信公眾號「尤而小屋」,作者Peter。轉(zhuǎn)載本文請聯(lián)系尤而小屋公眾號。
大家好,我是Peter呀~
從本文開始準備介紹Python中的常見數(shù)據(jù)結(jié)構(gòu):字符串、列表、集合、字典。其中字符串、列表、字典應(yīng)用非常頻繁,需要重點掌握,本文介紹的是字符串及相關(guān)操作和方法。最后的字符串3種格式化方法將在下篇文章詳細講解。
一、認識字符串
字符串在Python中是一種數(shù)據(jù)對象類型,用str表示,通常用單引號或者雙引號包裹起來(英文的半角符號)
字符串string,是有零個或者多個字符組成的有限串行,通常記為s=a[1]a[2]...a[m]
- strings = "hello world" # 雙引號
- strings
'hello world'
- type(strings)
str
- new_strings = 'hello python' # 單引號
- new_strings
通過type函數(shù)查看類型
- type(new_strings)
str
- type(100) # 數(shù)值型
int
- type("100") # 字符串類型
str
如果字符串本身內(nèi)容就有引號,我們有兩種解決方式:
- 雙引號包裹單引號
- 使用轉(zhuǎn)義字符
- # 如果字符串本身內(nèi)容也包含引號
- # 1、雙引號包裹單引號
- x = "I'm Peter!"
- x
"I'm Peter!"
- # 使用轉(zhuǎn)義字符\
- y = 'I\'m Peter'
- y
"I'm Peter!"
- # 3、使用r“字符內(nèi)容":原始字符串
- z = r"I'm Peter!"
- z
二、字符串基礎(chǔ)操作
2.1鍵盤輸入
鍵盤輸入的任何內(nèi)容都是字符串
- name = input("my name is: ")
my name is: Peter
- name # 返回的是字符串類型數(shù)據(jù)
'Peter'
- # 鍵盤輸入的都是字符串類型數(shù)據(jù)
- age = input("my age is: ")
my age is: 20
- type(age) # 返回的仍然是字符串
str
2.2變量與字符串
python中有這樣一句話:變量是無類型的,對象有類型
在下面的列子中,我們看到:變量x既可以是int類型,也可以是字符類型;但是數(shù)值5和字符串python都是有自己固定的數(shù)據(jù)類型。
- x = 5 # 變量x可以貼在int類型的數(shù)字5上:賦值語句
- x = "python" # 變量x也可以貼在字符串類型上
- # 通過賦值語句來表示變量和字符串對象之間的引用關(guān)系
- a = "hello-python"
- a
'hello-python'
- type(a)
str
2.3查看字符串地址
- id(a)
4516524144
- id(age)
4516499824
2.4原始字符串
用r開頭引起的字符串就是我們常用的原始字符串,放在里面的任何字符串都是表示它的原始含義,從此不需要轉(zhuǎn)義
- s = "hello \npython"
- print(s) # 發(fā)生換行
hello
python
- # 如何解決:1-使用轉(zhuǎn)義字符
- print("hello \\npython")
hello \npython
- # 2-使用r包裹起來
- print(r"hello \npython")
hello \npython
三、索引和切片
索引和切片是python中非常重要的一個概念,記住幾點:
- 索引左邊從0開始,右邊從-1開始
- 切片語法:start:end:step,step表示步長
3.1索引
使用的index()來查看某個字符的索引
- str1 = "python"
- id(str1)
4473172336
- str2 = "thonpy"
- id(str2)
4516506736
- # 尋找某個字符的索引index:索引從0開始
- str1.index("h")
3
- str1.index("n")
5
3.2切片
關(guān)于切片總結(jié)4點:
- 標準形式:start:stop:step
- 含頭不含尾:包含start部分,不包含stop部分
- 切片的時候,索引左邊從0開始,右邊從-1開始
- 步長step可正可負
- str3 = "learn python"
- str3
'learn python'
- # 標準切割
- str3[0:4:1] # 步長為1
'lear'
- str3[:4:1] # 開頭的0可以省略
'lear'
- str3[:4] # 步長1也可以省略
'lear'
- str3[0:4:2] # 步長為2
'la'
- str3[:10] # 步長為1,切到索引為10,不包含10
'learn pyth'
- str3[10:0:-2] # 步長為2
'otpna'
- str3.index("o") # 從索引10的o字符開始切割,往前切
10
四、字符串進階操作
4.1求長度
- len(str3)
12
4.2返回最值
每個字符都有自己對應(yīng)的數(shù)字編碼,通過比較數(shù)字就可以知道對應(yīng)字符的大小
- max(str3) # 根據(jù)ASCII碼的取值來決定
'y'
- min(str3)
' '
- ord("y") # 每個字符對應(yīng)的編碼
121
- ord("z")
122
- ord(" ")
32
- chr(121) # 數(shù)值對應(yīng)的字符:反編碼的過程
'y'
- "aa" > "ab" # 第一個字符相同就比較第二個
False
- "aac" > "aab" # c 大于 b
True
4.3判斷是否存在
- "p" in str3
True
- "q" in str3
False
- str3
'learn python'
4.4字符串重復(fù)
- str1
'python'
- str1 * 3
'pythonpythonpython'
4.5字符串連接
兩種方式:
- 通過+來實現(xiàn)
- 通過join來實現(xiàn)
- str1 * 3
'python'
- str4 = "learn " # 后面有個空格
- str4
'learn '
- str4 + str1
'learn python'
- "I" + " " + "am" + " Peter" # 使用+號多次連接
'I am Peter'
- # join連接
- " ".join(("learn","python")) # 連接符號為空格
'learn python'
- "+".join(("learn","python")) # 連接符號為+
'learn+python'
- " ".join(("I","am", "Peter"))
'I am Peter'
- 8 + "python" # 不同類型的數(shù)據(jù)不能相加,看下面的報錯
---------------------------------------------------------------------------
- TypeError Traceback (most recent call last)
- <ipython-input-56-d9d7a3d8267b> in <module>
- ----> 1 8 + "python" # 不同類型的數(shù)據(jù)不能相加
TypeError Traceback (most recent call last)
- "8" + "python"
'8python'
- str(8) + "python" # 使用str函數(shù)強制轉(zhuǎn)換
'8python'
五、常用字符串方法
5.1判讀是否全部為字母
- "python".isalpha()
True
- "8python".isalpha()
False
5.2分割字符串
- str5 = "My name is Peter"
- str5.split(" ") # 通過空格進行分割,得到的是列表(后面會介紹列表)
['My', 'name', 'is', 'Peter']
- str5.split() # 默認是空格切割,效果同上
['My', 'name', 'is', 'Peter']
- str5.split("") # 報錯空切割字符
---------------------------------------------------------------------------
- ValueError Traceback (most recent call last)
- <ipython-input-63-e39a6d8acc4b> in <module>
- ----> 1 str5.split("") # 報錯空切割字符
- str5.split("is") # 通過is來切割
['My name ', ' Peter']
5.3去掉字符串的空格
- strip():兩端的空格
- lstrip():左邊的空格
- rstrip():右邊的空格
- str6 = " python " # 左右各一個空格
- str6
' python '
- str6.strip()
'python'
- str6.rstrip()
' python'
- str6.lstrip()
'python '
- str6 # 原來的值保持不變
' python '
5.4字符大小寫轉(zhuǎn)化
python中實現(xiàn)各種類型的大小寫轉(zhuǎn)化
- upper():字母全部轉(zhuǎn)為大寫
- lower():字母全部轉(zhuǎn)為小寫
- capitalize():首字母全部轉(zhuǎn)為大寫
- title():字符串中所有單詞的首字母大寫,其他為小寫
- isupper():判斷字母是否全部轉(zhuǎn)為大寫
- islower():判斷字母是否全部轉(zhuǎn)為小寫
- istitle():判斷是否為標題模式,即字符串中所有單詞的首字母大寫,其他為小寫
- str7 = "this is Python" # 只有P是大寫
- str7
'this is Python'
- str7.upper() # 全部為大寫
'THIS IS PYTHON'
- str7.lower() # p也變成了小寫
'this is python'
- str7.capitalize() # 首字母T大寫
'This is python'
- str7.islower() # 是否全部為小寫
False
- str7.isupper() # 是否全部為大寫
False
- str7.istitle() # 是否為標題模式
False
- str7.title() # 轉(zhuǎn)成標題模式:每個單詞的首字母大寫
'This Is Python'
總結(jié)
字符串在Python中是非常高頻使用的是一種數(shù)據(jù)類型,從字符串的轉(zhuǎn)化、獲取字符串指定中的指定內(nèi)容、字符串的切片索引等都是必須掌握的知識點,希望本文對讀者有所幫助!