字符串(string)
Python中的字符串用單引號 ' 或雙引號 " 括起來,同時使用反斜杠 \ 轉義特殊字符。
'ABC' 與 "ABC" 完全等價。
示例:
s1="hello" # 雙引號 " 括起來 s2='hello' # 單引號 ' 括起來 s3='' # 空字符串 s4="" # 空字符串 s5='''hello''' # 三個單引號括起來 s6="""hello""" # 三個雙引號括起來 s7=str() # 空字符串 print(type(s1)) print(type(s2)) print(type(s3)) print(type(s4)) print(type(s5)) print(type(s6)) print(type(s7))
結果:
<class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'>
字符串的索引(index)
索引值以 0 為開始值,-1 為從末尾的開始位置。
示例:
str = 'ABCDEFG' print (str) # 輸出字符串 print (str[0]) # 輸出字符串第1個字符 print (str[3]) # 輸出字符串第4個字符 print (str[-1]) # 輸出字符串最后1個字符 print (str[-3]) # 輸出字符串最后第3個字符
運行結果:
ABCDEFG
A
D
G
E
字符串的切片(slice)[:]
字符串的截取子串的語法格式如下:
變量[頭下標:尾下標] # 特別注意,取頭不取尾
示例:
str = 'ABCDEFG' print (str) # 輸出字符串 print (str[0:-1]) # 輸出第一個到倒數第二個的所有字符 print (str[2:5]) # 輸出從第三個開始到第五個的字符 print (str[2:]) # 輸出從第三個開始的后的所有字符
執行以上程序會輸出如下結果:
ABCDEFG
ABCDEF
CDE
CDEFG
字符串運算符
假設 a="Hello",b="Python"
下表實例變量 a 值為字符串 "Hello",b 變量值為 "Python":
操作符 | 描述 | 實例 |
---|---|---|
+ | 字符串連接 |
>>>a + b
'HelloPython'
|
* | 重復輸出字符串 |
>>>a * 2
'HelloHello'
|
[ ] | 通過索引獲取字符串中字符 |
>>>a[1]
'e'
|
[ : ] | 截取字符串中的一部分 |
>>>a[1:4]
'ell'
|
in | 成員運算符 - 如果字符串中包含給定的字符返回 True |
>>>"H" in a
True
|
not in | 成員運算符 - 如果字符串中不包含給定的字符返回 True |
>>>"M" not in a
True
|
r/R | 原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思來使用,沒有轉義特殊或不能打印的字符。 原始字符串除在字符串的第一個引號前加上字母"r"(可以大小寫)以外,與普通字符串有着幾乎完全相同的語法。 |
>>>print(r'\n')
\n
>>> print(R'\n')
\n
|
加號 + 是字符串的連接符,把兩個字符串連接起來(拼接起來)。
星號 * 表示復制當前字符串,與之結合的數字為復制的次數(重復多次,復制多次)。
實例如下:
str = 'ABCDEFG' print (str) # 輸出字符串 print (str * 2) # 輸出字符串兩次,也可以寫成 print (2 * str) print (str + "TEST") # 連接字符串
執行以上程序會輸出如下結果:
ABCDEFG
ABCDEFGABCDEFG
ABCDEFGTEST
轉義字符(escape character)
Python 使用反斜杠 \ 轉義特殊字符,如果你不想讓反斜杠發生轉義,可以在字符串前面添加一個 r,表示原始字符串(r 指 raw,即 raw string,會自動將反斜杠轉義):
實例
>>> print('AB\nCD') AB CD >>> print(r'AB\nCD') AB\nCD
常見轉義字符
\\ | 反斜杠符號 Backslash | >>> print("\\") \ |
\' | 單引號 Single quote | >>> print('\'') ' |
\" | 雙引號 Double quote | >>> print("\"") " |
\n | 換行 Linefeed | >>> print("\n") >>> |
\t | 橫向制表符 Horizontal Tab | >>> print("Hello \t World!") Hello World! >>> |
\r | 回車,將 \r 后面的內容移到字符串開頭,並逐一替換開頭部分的字符,直至將 \r 后面的內容完全替換完成。Carriage Return | >>> print("Hello\rWorld!") World! >>> print('google taobao\r123456') 123456 taobao |
\ooo | 八進制數,o 代表 0~7 的字符,例如:\012 代表換行。octal value | >>> print("\110\145\154\154\157") Hello |
\xyy | 十六進制數,以 \x 開頭,y 代表的字符,例如:\x0a 代表換行. hex value | >>> print("\x48\x65\x6c\x6c\x6f") Hello |
注意,Python 沒有單獨的字符類型(character),一個字符就是長度為1的字符串。
>>> word = 'Python' >>> print(word[0], word[5]) P n >>> print(word[-1], word[-6]) n P
與 C 字符串不同的是,Python 字符串不能被改變。向一個索引位置賦值,比如 word[0] = 'm' 會導致錯誤。
ord()函數 可以將一個字符轉換為ASCII碼: 【Ordinal 英 /ˈɔːdɪn(ə)l/ 美 /ˈɔːrdən(ə)l/ 序數,序號,順序的,次序】
print(ord('a'))
#輸出 97
print(ord('0'))
#輸出 48
chr()函數 可以將一個ASCII碼轉換為字符:【character 英 /ˈkærəktə(r)/ 美 /ˈkærəktər/ n. 性格,品質;特色,特征;人物,角色;勇氣,毅力;某種人,古怪有趣的人;(書寫或印刷)符號,文字,字體;名譽,名聲 】
print(chr(97))
#輸出 'a'
print(chr(48))
#輸出 '0'
大寫A字母的ASCII值為65, 小寫a為97.
字符串內建函數
字符串有很多內建函數(https://www.cnblogs.com/emanlee/p/3616755.html),下面列出幾個常用的函數。
string.format()
格式化字符串 (https://www.cnblogs.com/emanlee/p/15816634.html)
string.lower()
轉換 string 中所有大寫字符為小寫.。
string.upper()
轉換 string 中的小寫字母為大寫。
string.replace(old, new, num=string.count(str1))
把 string 中的 old 替換成 new, 如果 num 指定,則替換不超過 num 次.。
示例:
>>> "abc".upper() 'ABC' >>> "ABC".lower() 'abc' >>> "ABCDEF".replace("AB","12345") '12345CDEF' >>>
REF
https://www.cnblogs.com/emanlee/p/3616755.html
https://www.runoob.com/python3/python3-data-type.html
https://www.runoob.com/python3/python3-string.html
https://www.cnblogs.com/mysterious-killer/p/10033830.html
https://blog.csdn.net/ewfwewef/article/details/109049451