自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python入門-字符串初相識

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

[[412886]]

 本文轉(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]

  1. strings = "hello world"  # 雙引號 
  2. strings 

'hello world'

  1. type(strings) 

str

  1. new_strings = 'hello python'  # 單引號 
  2. new_strings 

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

  1. type(new_strings)   

str

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

int

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

str

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

  • 雙引號包裹單引號
  • 使用轉(zhuǎn)義字符
  1. # 如果字符串本身內(nèi)容也包含引號 
  2. # 1、雙引號包裹單引號 
  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中有這樣一句話:變量是無類型的,對象有類型

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

  1. x = 5  # 變量x可以貼在int類型的數(shù)字5上:賦值語句 
  1. x  = "python"  # 變量x也可以貼在字符串類型上 
  1. # 通過賦值語句來表示變量和字符串對象之間的引用關(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中非常重要的一個概念,記住幾點:

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

3.1索引

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

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

4473172336

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

4516506736

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

3

  1. str1.index("n"

5

3.2切片

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

  • 標準形式:start:stop:step
  • 含頭不含尾:包含start部分,不包含stop部分
  • 切片的時候,索引左邊從0開始,右邊從-1開始
  • 步長step可正可負
  1. str3 = "learn python" 
  2. str3 

'learn python'

  1. # 標準切割 
  2.  
  3. str3[0:4:1] # 步長為1 

'lear'

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

'lear'

  1. str3[:4]  # 步長1也可以省略 

'lear'

  1. str3[0:4:2] # 步長為2 

'la'

  1. str3[:10]  # 步長為1,切到索引為10,不包含10 

'learn pyth'

  1. str3[10:0:-2] # 步長為2 

'otpna'

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

10

四、字符串進階操作

4.1求長度

  1. len(str3) 

12

4.2返回最值

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

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

'y'

  1. min(str3) 

' '

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

121

  1. ord("z"

122

  1. ord(" ")   

32

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

'y'

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

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字符串連接

兩種方式:

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

'python'

  1. str4 = "learn "  # 后面有個空格 
  2. str4 

'learn '

  1. str4 + str1 

'learn python'

  1. "I" + " " + "am" + " Peter"  # 使用+號多次連接 

'I am Peter'

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

'learn python'

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

'learn+python'

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

'I am Peter'

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

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

  1. TypeError                                 Traceback (most recent call last
  2.  
  3. <ipython-input-56-d9d7a3d8267b> in <module> 
  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ù)強制轉(zhuǎn)換 

'8python'

五、常用字符串方法

5.1判讀是否全部為字母

  1. "python".isalpha() 

True

  1. "8python".isalpha() 

False

5.2分割字符串

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

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

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

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

  1. str5.split("")  # 報錯空切割字符 

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

  1. ValueError                                Traceback (most recent call last
  2.  
  3. <ipython-input-63-e39a6d8acc4b> in <module> 
  4. ----> 1 str5.split("")  # 報錯空切割字符 

ValueError: empty separator

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

['My name ', ' Peter']

5.3去掉字符串的空格

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

' python '

  1. str6.strip() 

'python'

  1. str6.rstrip() 

' python'

  1. str6.lstrip() 

'python '

  1. 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():判斷是否為標題模式,即字符串中所有單詞的首字母大寫,其他為小寫
  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()  # 是否為標題模式 

False

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

'This Is Python'

總結(jié)

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

 

責任編輯:武曉燕 來源: 尤而小屋
相關(guān)推薦

2023-08-26 20:21:58

字符KotlinJava

2023-08-21 10:28:00

字符串字符Python

2024-05-09 08:28:10

Python字符串百分號格式化

2023-12-15 10:27:01

暴力匹配算法Python字符串

2022-05-18 11:35:17

Python字符串

2009-06-23 14:13:00

Java字符串

2020-08-11 14:29:31

Python字符串判斷

2010-03-09 16:16:55

Python字符串

2010-03-09 15:15:02

Python字符串類型

2024-05-10 09:26:26

Python字符串

2010-03-12 10:46:50

Python字符串

2017-01-17 15:47:18

2010-02-01 16:22:36

Python字符串操作

2024-04-01 08:41:39

字符串.NET

2021-03-08 08:23:24

Java字符串截取

2010-09-09 11:48:00

SQL函數(shù)字符串

2010-03-22 17:53:50

Python字符Python字符串

2010-11-26 09:51:54

MySQL字符串

2014-01-02 16:14:10

PostgreSQL字符串

2023-01-11 16:49:13

MySQL數(shù)據(jù)庫
點贊
收藏

51CTO技術(shù)棧公眾號