Python 資料結構 字串

2021-09-05 03:11:25 字數 855 閱讀 9132

簡單介紹一些常用的字串處理函式:

1、len()

2、strip()

3、find()

4、index()

5、count()

4、lower()

5、upper()

字串是python的一種不可變資料型別,一旦建立後,他所對應的記憶體空間的位址就確定下來,具體的記憶體位址可以通過id 函式進行檢視

對於字串的訪問,我們常常會結合 和 : 對字串進行訪問和切片,下邊是簡單訪問操作示例:

str1 = "hello world"

print(id(str1))

print(str1[0:5])

#輸出結果如下:

77407472

hello

首先建立hello world 字串,利用id函式輸出了字串記憶體位址為:77407472,利用 和 : 結合訪問str1的前5個元素為hello

字串索引格式為:strtest[開始索引:終止索引+1:步長]

上面的str1[0:5],只給出了起始索引和終止索引,這是步長為預設值1

當我們試圖改變字串str1中的第乙個元素時,因為字串為不可變型別,會發生報錯,如下:

str1[0] = "h"

typeerror traceback (most recent call last)

in ()

----> 1 str1[0] = "h"

typeerror: 'str' object does not support item assignment

python內建資料結構 字串

字串 乙個個 字元 不是位元組 組成的有序的序列 單引號 雙引號 三引號引住的字串行 可以被迭代,可以被索引 字串是不可變物件 python3,字串就是unicode 字串的join連線 string join iterable str 講課迭代物件連線起來,使用string作為分隔符 可迭代物件本...

python 內建資料結構字串

2.轉義字元 3.刪除字元和字串 二.字串的基本特性 字串的內建方法 字串或串由數字 字母 下劃線組成的一串字元。可以通過單引號 雙引號 三引號的方式建立它們。第一種方式 str1 hello world 第二種方式 str2 hello world 第三種方式 str3 hello world 第...

資料結構 字串

1 字串 include string.h include stdio.h include stdlib.h include math.h include time.h define ok 1 define error 0 define true 1 define false 0 define ma...