字符串 就是 一串字符,是編程語言中表示文本的數據類型git
在 Python 中能夠使用 一對雙引號 "
或者 一對單引號 '
定義一個字符串編程
雖然能夠使用 \"
或者 \'
作字符串的轉義,可是在實際開發中:api
若是字符串內部須要使用 "
,能夠使用 '
定義字符串編程語言
若是字符串內部須要使用 '
,能夠使用 "
定義字符串ide
能夠使用 索引 獲取一個字符串中 指定位置的字符,索引計數從 0 開始spa
也能夠使用 for
循環遍歷 字符串中每個字符code
大多數編程語言都是用
"
orm
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
方法 | 說明 |
---|---|
string.isspace() | 若是 string 中只包含空格,則返回 True |
string.isalnum() | 若是 string 至少有一個字符而且全部字符都是字母或數字則返回 True |
string.isalpha() | 若是 string 至少有一個字符而且全部字符都是字母則返回 True |
string.isdecimal() | 若是 string 只包含數字則返回 True,全角數字 |
string.isdigit() | 若是 string 只包含數字則返回 True,全角數字 、⑴ 、\u00b2 |
string.isnumeric() | 若是 string 只包含數字則返回 True,全角數字 ,漢字數字 |
string.istitle() | 若是 string 是標題化的(每一個單詞的首字母大寫)則返回 True |
string.islower() | 若是 string 中包含至少一個區分大小寫的字符,而且全部這些(區分大小寫的)字符都是小寫,則返回 True |
string.isupper() | 若是 string 中包含至少一個區分大小寫的字符,而且全部這些(區分大小寫的)字符都是大寫,則返回 True |
方法 | 說明 |
---|---|
string.startswith(str) | 檢查字符串是不是以 str 開頭,是則返回 True |
string.endswith(str) | 檢查字符串是不是以 str 結束,是則返回 True |
string.find(str, start=0, end=len(string)) | 檢測 str 是否包含在 string 中,若是 start 和 end 指定範圍,則檢查是否包含在指定範圍內,若是是返回開始的索引值,不然返回 -1 |
string.rfind(str, start=0, end=len(string)) | 相似於 find(),不過是從右邊開始查找 |
string.index(str, start=0, end=len(string)) | 跟 find() 方法相似,不過若是 str 不在 string 會報錯 |
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 次 |
方法 | 說明 |
---|---|
string.capitalize() | 把字符串的第一個字符大寫 |
string.title() | 把字符串的每一個單詞首字母大寫 |
string.lower() | 轉換 string 中全部大寫字符爲小寫 |
string.upper() | 轉換 string 中的小寫字母爲大寫 |
string.swapcase() | 翻轉 string 中的大小寫 |
方法 | 說明 |
---|---|
string.ljust(width) | 返回一個原字符串左對齊,並使用空格填充至長度 width 的新字符串 |
string.rjust(width) | 返回一個原字符串右對齊,並使用空格填充至長度 width 的新字符串 |
string.center(width) | 返回一個原字符串居中,並使用空格填充至長度 width 的新字符串 |
方法 | 說明 |
---|---|
string.lstrip() | 截掉 string 左邊(開始)的空白字符 |
string.rstrip() | 截掉 string 右邊(末尾)的空白字符 |
string.strip() | 截掉 string 左右兩邊的空白字符 |
方法 | 說明 |
---|---|
string.partition(str) | 把字符串 string 分紅一個 3 元素的元組 (str前面, str, str後面) |
string.rpartition(str) | 相似於 partition() 方法,不過是從右邊開始查找 |
string.split(str="", num) | 以 str 爲分隔符拆分 string,若是 num 有指定值,則僅分隔 num + 1 個子字符串,str 默認包含 '\r', '\t', '\n' 和空格 |
string.splitlines() | 按照行('\r', '\n', '\r\n')分隔,返回一個包含各行做爲元素的列表 |
string.join(seq) | 以 string 做爲分隔符,將 seq 中全部的元素(的字符串表示)合併爲一個新的字符串 |