01-Python 中的數據類型-02-字符串類型

總體 要講的大綱內容 如下

  • 數字類型- int float complex
  • 字符串類型 Text Sequence Type- str
  • 序列類型 - list tuple range
  • 集合類型 - set frozenset
  • 上下文管理器類型 - 比較複雜暫時 不用掌握
  • 文本序列類型
  • 二進制序列類型 bytes bytesarray memoryview
  • 真值檢測
  • 比較運算符
  • 邏輯運算符
  • 如何判斷 一個對象 是什麼類型- type(xxx)

今天 繼續講 基礎類型的字符串類型 , 字符串類型是比較常見的。

生活中 東西的名稱 ,蘋果 對應就是 Apple ,橘子 orange

字符串的定義

在Python中 字符串 是如何定義的呢?

一般來說 有三種方式定義一個字符串 。 單引號, 雙引號,和三單引號,三雙引號

注意 這裏 單引號,雙引號,都是英文字符, 不是中文字符。

name = 'frank'

name2 = "frank2"

name3 = '''frank'''

name4 = """frank4"""

print(name, type(name))
print(name2, type(name2))
print(name3, type(name3))
print(name4, type(name4))

img01

這種幾種方式 幾乎沒有區別。有時候 可以替換。

假設你的字符串有' 這個時候,就可以用雙引號。

>>> sentence="Frank's book is here."
>>> sentence
"Frank's book is here."

>>> type(sentence)
<class 'str'>

還有你的字符串比較長,一行很難放下,有多行的時候。

這個時候可以用三引號

sentence="""The debugger caught an exception in your WSGI application.
You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, 
you can click on the "Traceback" headline. 
From the text traceback you can also create a paste of it. 
For code execution mouse-over the frame you want to debug and click on the console icon on the right side."""

>>> sentence="""The debugger caught an exception in your WSGI application.
... You can now look at the traceback which led to the error.
... To switch between the interactive traceback and the plaintext one, 
... you can click on the "Traceback" headline. 
... From the text traceback you can also create a paste of it. 
... For code execution mouse-over the frame you want to debug and click on the console icon on the right side."""
>>> 

字符串 常見的方法(操作)

capitalize ,title

str.capitalize() 這個方法 返回一個字符串 , 這個字符串相對於str 開頭第一個字符大寫

str.title() 每個單詞的首字母 大寫

看下面例子

>>> s = 'frank  aaa  bbb'
>>> s.capitalize()
'Frank  aaa  bbb'
>>> s.title()
'Frank  Aaa  Bbb'

encoding

str.encoding() 字符串可以進行編碼,默認utf-8 編碼,簡單理解 字符串可以轉換不同的編碼格式 。utf-8用的比較多.

>>> name = 'frank'
>>> 
>>> name.encode(encoding="utf-8")
b'frank'
>>> name2= name.encode(encoding="utf-8")
>>> type(name2)
<class 'bytes'>

這裏 發現 經過 str.encode 把 一個str 類型 轉換成了一個 btyes 類型。bytes 類型 也是python的基礎數據類型,之後會說到 。

現在要記得 str 如何轉成bytes 類型 使用 encode 這個方法 。

位置概念

補充一點:

str 這個 數據類型可以有"位置" 的概念 ,默認第一個位置 是從0 開始的。

>>> name='frank'
>>> name[0]
'f'
>>> name[1]
'r'
>>> name[4]
'k'
>>> name[3]
'n'

從上面可以看出第0 號位置是’f’ , 第四號位置是 ‘k’ .

注意 對應 frank 這個字符串 ,最大的位置是4,能不能取5呢? 肯定不能,因爲位置5 已經超出了 frank 的範圍。看下面的例子

>>> name[5]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: string index out of range

有的時候 我並不獲取一個字符,而是獲取 一段 , python中一個東西 叫切片 使用 方法 str[a:b] , a<=x<b 這個範圍

>>> sentence
'The debugger caught an exception in your WSGI application.'

>>> sentence[0:3]
'The'
>>> sentence[4:12]
'debugger'

find,rfind

str.find() 尋找一個字符串 是否在 str 中 ,如果在 返回對應最小的位置的下標(位置),如果沒有找到 返回 -1

>>> sentence="""The debugger caught an exception in your WSGI application."""
>>> sentence
'The debugger caught an exception in your WSGI application.'
>>> sentence.find('an')
20
>>> sentence.find('the')
-1
>>> sentence.find('Frank')
-1
>>> sentence.find('T')
0
>>> sentence.find('debug')
4



str.rfind() 尋找一個字符串 是否在 str 中 ,如果在 返回對應最大的位置的下標(位置),如果沒有找到 返回 -1

>>> # 再來看一個例子
>>> sentence ='I like swimming and I like sports'
>>> sentence.find('like')
2
>>> sentence[2]
'l'
>>> sentence.rfind('like')
22
>>> sentence[2:6]
'like'
>>> sentence[22:26]
'like'

index,rindex

str.index 這個方法 和find 相似,唯一的區別是 當尋找的子串沒有找到的時候會報錯,拋出 ValueError的異常。

>>> sentence
'The debugger caught an exception in your WSGI application.'
>>> sentence.index('an')
20
>>> sentence.index('in')
33
>>> sentence.index('on')
30
>>> sentence.index('over')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: substring not found

str.rindex() 和 str.find() 方法類似, 唯一的區別是 沒有找到的情況下,也會拋出一個 ValueError的異常

startswith ,endswith

str.startswith(prefix) 判斷 字符串 是否是prefix開頭,返回 True ,False

str.endswith(suffix) 判斷 字符串 是否是suffx 結尾返回 True ,False

>>> s ='apple  aaa bbb ccc  dd'
>>> s.startswith('a')
True
>>> s.startswith('app')
True
>>> s.startswith('apps')
False
>>> s.endswith('dd')
True
>>> s.endswith('d')
True
>>> s.endswith('df')
False

len 獲取字符串長度

len(s) 獲取字符串的長度

>>> s
'apple  aaa bbb ccc  dd'
>>> len(s)
22

replace

str.replace(old, new[, count]) 返回一個 將old 替換爲new 的一個字符串 ,如果 count 給定了值,只替換 count 次。如果沒有 給定count 就是全部替換。

>>> # 把 A 替換成 a 
>>> string ='aaaaAaaaAaaaAAa'
>>> string.replace('A','a')
'aaaaaaaaaaaaaaa'
>>> # 最多替換2次
>>> string.replace('A','a',2)
'aaaaaaaaaaaaAAa'

strip,lstrip,rstrip

有時候 一個字符串 沒有那麼規整,比如前後都有空格,你想去掉 前面,或者的空格 保留中間的部分 .

str.lstrip() 去掉 左邊空格,返回一個字符串

str.rstrip() 去掉 右邊空格,返回一個字符串

str.strip() 去掉 兩邊 邊空格,返回一個字符串

>>> s ='       aaa bbb ccc    '
>>> s
'       aaa bbb ccc    '
>>> s.lstrip()
'aaa bbb ccc    '
>>> s
'       aaa bbb ccc    '
>>> s.rstrip()
'       aaa bbb ccc'
>>> s.strip()
'aaa bbb ccc'

upper,lower

大小寫 轉換

str.upper() 轉換爲 大寫,返回一個字符串

str.lower() 轉換爲小寫,返回一個字符串

>>> 'abc'.upper()
'ABC'
>>> 
>>> 'Frank'.upper()
'FRANK'


>>> 'FRANK'.lower()
'frank'
>>> 'FrAnk'.lower()
'frank'

str.islower()

str.isupper()

判斷是不是全是 大寫 或者小寫,返回 布爾值True, False

>>> 'frank'.islower()
True
>>> 'frank'.isupper()
False
>>> 'FRANK'.isupper()
True

swapcase

str.swapcase() 這個方法 有點意思 ,就是改變字符的大小寫。

原本大寫字符 -> 小寫字符

原本小寫字符 變成 大寫字符

>>> 'aaBBccDD'.swapcase()
'AAbbCCdd'

+ 連接字符串

連接兩個字符串 用 +

>>> name ='frank'
>>> hobby = 'swimming'
>>> verb = 'likes'
>>> name + verb + hobby
'franklikesswimming'
>>> # 好醜,重新連接
>>> name +' '+ verb+' ' + hobby +'.'
'frank likes swimming.'

join

str.join(iterable) 返回一個字符串,該字符串是可迭代的字符串的串聯,有點抽象,舉個例子

>>> hello ='hello'
>>> world='world'


>>> ','.join(world)
'w,o,r,l,d'
>>> hello.join(world)
'whelloohellorhellolhellod'

比如用, join 一個字符串,就是 用逗號將world 每一個字符連接起來 。

hello.join (world) 就是用 hello 把 world 每一個字符 連接起來

‘whelloohellorhellolhellod’ 就是下面的樣子 。

比如用加號 把 hello 連接起來

>>> '+'.join(hello)
'h+e+l+l+o'

count

str.count(sub [, start[, end]]) 在 [start,end ] 範圍內 尋找 沒有子串sub ,如果有的話,出現的次數。如果沒有返回0 , start ,end 如果不指定的話,默認 搜索整個字符串的範圍 ,即 start=0,end =len(str)-1

注意: str 索引 是從0 開始的。

>>> sentence ="hello hello world world hello hello"
>>> sentence.count('hello')
4
>>> sentence.count('aaa')
0


>>> sentence[0:17]
'hello hello world'
>>> sentence.count('hello',0,17)
2

split ,splitlines

str.split()

str.split(sep=None, maxsplit=-1) 分開的意思 拆分字符串,

參數sep 就是要分隔字符的標識,maxsplit 拆分的次數,默認值 是-1,就是儘可能大的次數拆分這個字符串。

>>> #用逗號拆分
>>> 'one,two,three,four'.split(',')
['one', 'two', 'three', 'four']
>>> nums='one,two,three,four'.split(',')
>>> nums
['one', 'two', 'three', 'four']
>>> type(nums)
<class 'list'>



>>> 'one,two,three,four'.split(',',maxsplit=2)
['one', 'two', 'three,four']

首先 就是用逗號拆分這個字符串,發現這個字符串全部通過逗號拆開了, 並且 這些值 放到了[ ] 裏面,

通過 type 查看這個 類型 發現是 list, 現在 你又發現了一種 數據類型 叫list。 它可以保存一系列的數據。

maxsplit 設置 切分次數。 上面的例子 設置2 ,那麼之後的字符串就單獨 放在一個一起了。

splitlines 這個是以 ‘\n’ 作爲換行符 ,並且返回一個list ,但是 如果 這個字符以最後一個\n 結尾,

兩個 方法 稍微有點區別,如果使用split(’\n’)會被拆成兩條數據。 而 splitlines 只會是一條數據, 這就是有點區別的地方 。

>>> 'aaa\nbbb\nccc\nddd'.splitlines()
['aaa', 'bbb', 'ccc', 'ddd']

>>> 'aaa\nbbb\nccc\nddd'.split('\n')
['aaa', 'bbb', 'ccc', 'ddd']


>>> 'one line\n'.split('\n')
['one line', '']
>>> 'one line\n'.splitlines()
['one line']

isxxxx

isxxx 系列 判斷 是不是某些 特殊的值, 返回 True ,False

這些 平常用到不是特別多,但是用到的時候,只要去查一些 文檔就好了。

str.isascii() 是不是ASCII 嗎?
str.isalnum() 所有的字符是否都是 數字
str.isalpha() 所有的字符是不是都是 字母
str.isdecimal() 所有的字符是不是都是 小數的字符
str.isidentifier() 所有的字符 都是標識符
str.isspace() 所有的字符 是不是 都是 空格
str.isprintable() 所有的字符 是不是都是 可以打印的

name = '1.343'
name.isascii()
name.isalnum()
name.isalpha()
name.isdecimal()
name.isidentifier()
name.isspace()
name.isprintable()

總結

​ 今天 主要講了字符串的表示,以及常用方法,查找,拼接,替換,統計,大小寫轉換等。

這裏可能 你不能把所有的方法都能記住, 但是 用到的時候你知道如何查文檔就可以了。還有今天接觸了兩種數據類型, 一種是bytes 類型, 一種是 list 類型 ,還記得 他們是如何得到的嗎? 如果忘記了,趕緊翻上去 ,看看哦! 加油!

參考文檔

doc str
identifiers
count
methods

分享快樂,留住感動. 2020-03-18 20:29:39 --frank
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章