Python資料型別 02 字串

2022-07-13 10:39:08 字數 4566 閱讀 7189

本文主要記錄字串的相關知識,包括字串的定義特點,常用方法和

請知悉:

計算機中,一切皆為物件

世界萬物,皆為物件,一切物件皆可分類

類似"hello world"就是乙個字串

定義:是乙個有序的字元的集合,用於儲存和表示基本的文字資訊,一對單雙或三引號中間包含的內容成為字串

注意:變數宣告中,不加引號的字串表示變數,name=jack 會出錯

有序,不可變

字串是特殊的元組,可以切片

字串是 %s,整數 %d,浮點數%f

name = "

jack

"print ("

i am %s,how are you?

" %name)

--->i am jack,how are you?

a = '

helllo

'b = '

world

'print(a +b)

--->'

hello world

'

注意:python中的字串在c語言中體現為乙個字元陣列,每次建立字串時候需要在記憶體中開闢一塊連續的空間,一旦需要修改字串,就需要再次開闢空間,會占用過多記憶體空間

原理:使用加號連線2個字串會呼叫靜態函式string_concat(register pystringobject *a ,register pyobject * b),在這個函式中會開闢一塊大小是a+b的記憶體的和的儲存單元,然後將a,b字串拷貝進去。如果是n個字串相連 那麼會開闢n-1次記憶體,是非常耗費資源的。

name='

jack

'print(name*5)

-->jack jack jack jack jack

注意:「+」和「*」只能在字串之間進行操作,不能跨資料型別

連線2個字串,可指定連線符號,常用

a='

hello

'b='

####

'print

(a.join(b))

--->#

hello #hello #hello #

s = '

hello world

'print(s.count('o'

))--->2

刪除字串兩邊的指定字元,預設為空格,\t為tab,\n為換行符

s = '

hello world

's.strip()

--->'

hello world

'

刪除字串左邊的指定字元,預設為空格

s = '

hello

's.lstrip()

--->'

hello

'

刪除字串右邊指定字元,預設為空格

s='

hello

's.rstrip()

--->'

hello

'

功能相同,區別在於find()查詢失敗會返回-1,不會影響程式執行。

一般用find!=-1或者find>-1來作為判斷條件。

str.find:檢測字串中是否包含子字串str,可指定範圍

s='

hello world

'print(s.find('

l')) #

第乙個索引為2

--->2

print(s.find('

x')) #

找不到返回-1

--->-1

str.index:檢測字串中是否包含子字串str,可指定範圍

s='

hello world

'print(s.index('l'

))-->2

print(s.index('x'

))-----返回------traceback (most recent call last):

file

"d:/pyprojects/1.svndata/test.py

", line 32, in

print(s.index('x'

))valueerror: substring

not found

s = '

hello world

'print

(len(s))

--->11

是否包含指定字串

s = '

hello world

'print('

hello'in

s)--->true

print('

123'

notin

s)--->true

s = '

hello world

'print

(s.lower())

--->hello world

s = '

hello world

'print

(s.upper())

--->hello world

s = '

hello world

'print

(s.swapcase())

--->hello world

可指定字串長度以及兩邊填充的字元

s = '

hello world

'print(s.center(40, '*'

))--->**************hello world***************

這類函式在string模組中沒有,且返回的都是布林值

str.isdigit()                  #

是否全是數字,並至少有乙個字元

str.isalpha() #

是否全是字母,並至少有乙個字元

str.isspace() #

是否全是空白字元,並至少有乙個字元

str.isalnum() #

是否全是字母和數字,並至少有乙個字元

str.islower() #

str中的字母是否全是小寫

str.isupper() #

str中的字母是否全是大寫

str.istitle() #

str是否是首字母大寫的

str.startswith("hehe") #

是否以hehe開頭

str.endswith("haha")    #

是否以haha結尾

str = '

0123456789

'print(str[0:3])     #

擷取第一位到第三位的字元

print(str[:])   #

擷取字串的全部字元

print(str[6:])     #

擷取第七個字元到結尾

print(str[:-3])    #

擷取從頭開始到倒數第三個字元之前

print(str[2])     #

擷取第三個字元

print(str[-1])     #

擷取倒數第乙個字元

print(str[::-1])    #

創造乙個與原字串順序相反的字串

print(str[-3:-1])   #

擷取倒數第三位與倒數第一位之前的字元

--->78

print(str[-3:])    #

擷取倒數第三位到結尾

print(str[:-5:-3]) #

逆序擷取,擷取倒數第五位數與倒數第三位數之間

--->96

注意:1)字串物件是不可改變的,也就是說在python建立乙個字串後,你不能把這個字元中的某一部分改變。

2)任何上面的函式改變了字串後,都會返回乙個新的字串,但是原字串並沒有變。

3)字串是可以通過下標來進行取值的,但是由於字串是不可變變數,不能通過下標來修改它的值(形式如字串[下標])

4)下標(索引值)從左邊,從0開始,最大下標值是字串長度減1,即len(string) - 1

1.可變型別:在id不變的情況下,value可以變,則稱為可變型別,如列表,字典

2. 不可變型別:value一旦改變,id也改變,則稱為不可變型別(id變,意味著建立了新的記憶體空間)

---------- 完畢,呵呵呵呵 -----------

python學習之旅 02字串

所謂字串,就是由零個或多個字元組成的有限序列,一般記為。在python程式中,如果我們把單個或多個字元用單引號或者雙引號包圍起來,就可以表示乙個字串。s1 hello,world s2 hello,world 以三個雙引號或單引號開頭的字串可以折行 s3 hello,world print s1,s...

python筆記02 字串操作

str1 www str1就是字串了 一定用引號 或者直接使用 字串.來呼叫內部的方法 capitalize 方法 開頭大寫 print wsy www capitalize wsy www swapcase 方法 全都大寫 print wsy ada swapcase wsy ada isuppe...

Python資料型別 字串型別

變數名 str 變數值 msg hello world print msg 0 print msg 1 msg hello n print len msg msg hello world print ello in msg print lo w not in msg res print hello ...