字符串的表示
- ‘’:一對單引號
- “”:一對雙引號
- ‘’‘ ’‘’:三個單引號
- “”“ ”“”:三個雙引號
- 轉義字符
下標和切片
- 下標
name = 'abcdef'
print(name[0])
#打印:a
- 遍歷
- while語句遍歷
msg = 'hello world'
i = 0
while i < len(msg):
print(msg[i])
i += 1
- for語句遍歷
- msg = 'hello world'
for x in msg:
print(x)
- 切片
- 切片的語法:
- [起始:結束:步長]
- 含頭不含尾
s = 'Hello World!'
s[:]
- s = 'hello world'
print(s[1:5:2])
#輸出:'el'
注意:在Python中,字符串是不可變的!所有的字符串相關方法,都不會改變原有的字符串,都是返回一個結果,在這個新的返回值裏,保留了執行後的結果!
字符串的常見操作:
字符串的常見操作包括:
獲取長度:len
查找內容:find,index,rfind,rindex
判斷:startswith,endswith,isalpha,isdigit,isalnum,isspace
計算出現次數:count
替換內容:replace
切割字符串:split,rsplit,splitlines,partition,rpartition
修改大小寫:capitalize,title,upper,lower
空格處理:ljust,rjust,center,lstrip,rstrip,strip
字符串拼接:join
1. 獲取長度
2.查找內容
- s.find(’字符‘[,起始位置,結束爲止]),
返回值爲int類型的值,如果沒有找到,返回-1- S.find(sub[, start[, end]]) -> int
Return -1 on failure.
print(mystr.find('好風光')) # 10
print(mystr.find('你好')) # -1
-
rfind()
- s.rfind(’字符‘[,起始位置,結束爲止]),
返回值爲int類型的值,如果沒有找到,返回-1 - S.rfind(sub[, start[, end]]) -> int
Return -1 on failure.
- s.rfind(’字符‘[,起始位置,結束爲止]),
print(mystr.rfind('好')) # 14
- s.index(’字符‘[,起始位置,結束位置])
- S.index(sub[, start[, end]]) -> int
print(mystr.index('風')) # 11
3.判斷
- s.startwith(‘字符’[,起始位置,結束爲止])
返回一個布爾類型的值- S.startswith(prefix[, start[, end]]) -> bool
print(mystr.startswith('今')) # True
print(mystr.startswith('今日')) # False
- s.endwith(‘字符’[,起始位置,結束爲止])
返回一個布爾類型的值- S.endswith(suffix[, start[, end]]) -> bool
print(mystr.endswith('好風光')) # True
print(mystr.endswith('好日子')) # False
- 如果字符全爲字母,返回True,否則返回假False
- Return True if the string is an alphabetic string, False otherwise.
mystr = 'hello'
print(mystr.isalpha()) # True
mystr = 'hello world'
print(mystr.isalpha()) # False 因爲中間有空格
isdigit()
- 如果字符串全爲數字,則返回True,否則返回False
- Return True if the string is a digit string, False otherwise.
mystr = '1234'
print(mystr.isdigit()) # True
mystr = '123.4'
print(mystr.isdigit()) # False
mystr = '-1234'
print(mystr.isdigit()) # False
isalnum()
- 如果字符串是由數字或者字母組成,則返回True,否則返回False
- Return True if the string is an alpha-numeric string, False otherwise.
- mystr = 'abcd'
print(mystr.isalnum()) # True
mystr = 'abcd1234'
print(mystr.isalnum()) # True
mystr = '_abcd1234'
print(mystr.isalnum()) # False
isspace()
- 如果字符串裏只有空格,則返回True,否則返回False
- Return True if the string is a whitespace string, False otherwise.
mystr = ''
print(mystr.isspace()) # False mystr 是一個空字符串
mystr = ' '
print(mystr.isspace()) # True mystr 只有空格
mystr = ' d'
print(mystr.isspace()) # False mystr 除了空格還有其它內容
4. 計算出現次數
count()
mystr = '今天天氣好晴朗,處處好風光呀好風光'
print(mystr.count('好')) # '好'字出現三次
5. 替換內容
替換字符串中的指定內容,如果指定次數count,則替換不會超過cout次
replace
new_str = mystr.replace('好','壞')
print(mystr) # 今天天氣好晴朗,處處好風光呀好風光
print(new_str) # 今天天氣壞晴朗,處處壞風光呀壞風光
new_str = mystr.replace('好', '壞', 2)
print(mystr) # 今天天氣好晴朗,處處好風光呀好風光
print(new_str) # 今天天氣壞晴朗,處處壞風光呀好風光
6.切割字符串
split
有sep(以sep爲分隔符) 和 maxsplit(最大分割次數) 可選參數
result = mystr.split() # 沒有指定分割符,默認使用空格
print(result) # ['今天天氣好晴朗,處處好風光呀好風光']
result = mystr.split('好') # 以 '好' 爲分割符
print(result)#['今天天氣', '晴朗,處處', '風光呀', '風光']
rsplit
和split基本一致,但是從右往左分
mystr = '今天天氣好晴朗,處處好風光呀好風光'
print(mystr.rsplit('好',1)) #['今天天氣好晴朗,處處好風光呀', '風光']
splitlines
按照行分隔,返回一個包含各行作爲元素的列表。
mystr = 'hello \nworld'
print(mystr.splitlines())#['hello ', 'world']
partition
把mystr以str分割成三部分,str前,str和str後,三部分組成一個元組
mystr = '今天天氣好晴朗,處處好風光呀好風光'
print(mystr.partition('好'))#('今天天氣', '好', '晴朗,處處好風光呀好風光')
rpartition
mystr = '2012.1.2.txt'
print(mystr.rpartition('.')) # ('2012.1.2', '.', 'txt') rpatition:從右邊開始
7. 修改大小
mystr = ‘hello world’
capitalize
第一個單詞的首字母大寫。
- print(mystr.capitalize()) # Hello wrld
title
每個單詞的首字母大寫。
print(mystr.title()) # Hello World
lower
所有都變成小寫。
- mystr = 'hElLo WorLD'
print(mystr.lower()) # hello world
upper
所有都變成大寫。
mystr = 'hello world'
print(mystr.upper()) #HELLO WORLD
8. 空格處理
- ljust
- rjust
- center
- lstrip
- rstrip
- strip
9.字符串拼接
join
作用:可以把列表或者元組快速的轉變成爲字符串,並且以指定的字符分隔。
'txt = '_'
print(txt.join(['hi','hello','good'])) #hi_hello_good
print(txt.join(('good','hi','hello'))) #good_hi_hello
```