04. 字符串
4.1 字符串的定義
- 字符串 就是 一串字符柒瓣,是編程語言中表示文本的數(shù)據(jù)類型
- 在 Python 中可以使用 一對(duì)雙引號(hào)
"
或者 一對(duì)單引號(hào)'
定義一個(gè)字符串- 雖然可以使用
\"
或者\'
做字符串的轉(zhuǎn)義,但是在實(shí)際開發(fā)中:- 如果字符串內(nèi)部需要使用
"
舒憾,可以使用'
定義字符串 - 如果字符串內(nèi)部需要使用
'
,可以使用"
定義字符串
- 如果字符串內(nèi)部需要使用
- 雖然可以使用
- 可以使用 索引 獲取一個(gè)字符串中 指定位置的字符,索引計(jì)數(shù)從 0 開始
- 也可以使用
for
循環(huán)遍歷 字符串中每一個(gè)字符
大多數(shù)編程語言都是用
"
來定義字符串
string = "Hello Python"
for c in string:
print(c)
4.2 字符串的常用操作
- 在
ipython3
中定義一個(gè) 字符串,例如:hello_str = ""
- 輸入
hello_str.
按下TAB
鍵解阅,ipython
會(huì)提示 字符串 能夠使用的 方法 如下:
In [1]: hello_str.
hello_str.capitalize hello_str.isidentifier hello_str.rindex
hello_str.casefold hello_str.islower hello_str.rjust
hello_str.center hello_str.isnumeric hello_str.rpartition
hello_str.count hello_str.isprintable hello_str.rsplit
hello_str.encode hello_str.isspace hello_str.rstrip
hello_str.endswith hello_str.istitle hello_str.split
hello_str.expandtabs hello_str.isupper hello_str.splitlines
hello_str.find hello_str.join hello_str.startswith
hello_str.format hello_str.ljust hello_str.strip
hello_str.format_map hello_str.lower hello_str.swapcase
hello_str.index hello_str.lstrip hello_str.title
hello_str.isalnum hello_str.maketrans hello_str.translate
hello_str.isalpha hello_str.partition hello_str.upper
hello_str.isdecimal hello_str.replace hello_str.zfill
hello_str.isdigit hello_str.rfind
提示:正是因?yàn)?python 內(nèi)置提供的方法足夠多,才使得在開發(fā)時(shí)泌霍,能夠針對(duì)字符串進(jìn)行更加靈活的操作瓮钥!應(yīng)對(duì)更多的開發(fā)需求!
1) 判斷類型 - 9
方法 | 說明 |
---|---|
string.isspace() | 如果 string 中只包含空格烹吵,則返回 True |
string.isalnum() | 如果 string 至少有一個(gè)字符并且所有字符都是字母或數(shù)字則返回 True |
string.isalpha() | 如果 string 至少有一個(gè)字符并且所有字符都是字母則返回 True |
string.isdecimal() | 如果 string 只包含數(shù)字則返回 True碉熄,全角數(shù)字
|
string.isdigit() | 如果 string 只包含數(shù)字則返回 True,全角數(shù)字 肋拔、⑴ 锈津、\u00b2
|
string.isnumeric() | 如果 string 只包含數(shù)字則返回 True,全角數(shù)字 凉蜂,漢字?jǐn)?shù)字
|
string.istitle() | 如果 string 是標(biāo)題化的(每個(gè)單詞的首字母大寫)則返回 True |
string.islower() | 如果 string 中包含至少一個(gè)區(qū)分大小寫的字符琼梆,并且所有這些(區(qū)分大小寫的)字符都是小寫,則返回 True |
string.isupper() | 如果 string 中包含至少一個(gè)區(qū)分大小寫的字符窿吩,并且所有這些(區(qū)分大小寫的)字符都是大寫茎杂,則返回 True |
2) 查找和替換 - 7
方法 | 說明 |
---|---|
string.startswith(str) | 檢查字符串是否是以 str 開頭,是則返回 True |
string.endswith(str) | 檢查字符串是否是以 str 結(jié)束纫雁,是則返回 True |
string.find(str, start=0, end=len(string)) | 檢測(cè) str 是否包含在 string 中煌往,如果 start 和 end 指定范圍,則檢查是否包含在指定范圍內(nèi)轧邪,如果是返回開始的索引值刽脖,否則返回 -1
|
string.rfind(str, start=0, end=len(string)) | 類似于 find()羞海,不過是從右邊開始查找 |
string.index(str, start=0, end=len(string)) | 跟 find() 方法類似,不過如果 str 不在 string 會(huì)報(bào)錯(cuò) |
string.rindex(str, start=0, end=len(string)) | 類似于 index()曲管,不過是從右邊開始 |
string.replace(old_str, new_str, num=string.count(old)) | 把 string 中的 old_str 替換成 new_str却邓,如果 num 指定,則替換不超過 num 次 |
3) 大小寫轉(zhuǎn)換 - 5
方法 | 說明 |
---|---|
string.capitalize() | 把字符串的第一個(gè)字符大寫 |
string.title() | 把字符串的每個(gè)單詞首字母大寫 |
string.lower() | 轉(zhuǎn)換 string 中所有大寫字符為小寫 |
string.upper() | 轉(zhuǎn)換 string 中的小寫字母為大寫 |
string.swapcase() | 翻轉(zhuǎn) string 中的大小寫 |
4) 文本對(duì)齊 - 3
方法 | 說明 |
---|---|
string.ljust(width) | 返回一個(gè)原字符串左對(duì)齊院水,并使用空格填充至長(zhǎng)度 width 的新字符串 |
string.rjust(width) | 返回一個(gè)原字符串右對(duì)齊腊徙,并使用空格填充至長(zhǎng)度 width 的新字符串 |
string.center(width) | 返回一個(gè)原字符串居中,并使用空格填充至長(zhǎng)度 width 的新字符串 |
5) 去除空白字符 - 3
方法 | 說明 |
---|---|
string.lstrip() | 截掉 string 左邊(開始)的空白字符 |
string.rstrip() | 截掉 string 右邊(末尾)的空白字符 |
string.strip() | 截掉 string 左右兩邊的空白字符 |
6) 拆分和連接 - 5
方法 | 說明 |
---|---|
string.partition(str) | 把字符串 string 分成一個(gè) 3 元素的元組 (str前面, str, str后面) |
string.rpartition(str) | 類似于 partition() 方法檬某,不過是從右邊開始查找 |
string.split(str="", num) | 以 str 為分隔符拆分 string撬腾,如果 num 有指定值,則僅分隔 num + 1 個(gè)子字符串橙喘,str 默認(rèn)包含 '\r', '\t', '\n' 和空格 |
string.splitlines() | 按照行('\r', '\n', '\r\n')分隔,返回一個(gè)包含各行作為元素的列表 |
string.join(seq) | 以 string 作為分隔符胶逢,將 seq 中所有的元素(的字符串表示)合并為一個(gè)新的字符串 |