參考python爬蟲網路實戰(第二版)胡松濤
s.lower():
#字母大寫轉換成小寫
s.upper(
)#字母小寫轉換成大寫
s.swapcase(
)#字母大寫轉換成小寫,小寫轉換成成大寫
s.title(
)#將首字母大寫
s.find(substr,
[start,
[end]])
#返回s**現substr的第乙個字母的標號,start和end的作用相當於在s[start:end]中搜尋
s.count(substr,
[start,
[end]])
:#計算substr在s**現的次數
s.replace(older,newstr,
[count]):
#把s中的older替換成newstr,count為替換的次數
s.strip(
[chars]):
#把s兩端chars中有的字元全部去掉,一般用來去掉空格
s.split(
[sep,
[maxsplit]])
:#以sep為分隔,把s分成乙個list,maxsplit為分割的次數,預設空白字元
s.join(sep)
:#把sep代表的序列用s連線起來
s.decode(
[encoding]):
#將以encoding編碼的s解碼成unicode編碼
s.isalpha():
#s是否全是字母,至少有乙個字元
s.isdigit():
#是否全是數字,至少有乙個字元
s.isspace():
#s是否全是空白字元,至少有乙個字元
s.islower():
#s是否全是小寫
s.isupper():
#s是否全是大寫
s.istitle():
#s是否是首字母大寫
#-*-coding:utf-8 -*-
defstrcase()
:print
("演示字串的大小寫轉換\n"
) s =
'i am a cool boy'
print
("大寫轉換成小寫:"
,s.lower())
print
("小寫轉換成大寫:"
,s.upper())
print
("大小寫相互轉換:"
,s.swapcase())
print
("首字母大寫:"
,s.title())
defstrfind()
:print
("字串的替換的搜尋\n"
) s =
' you are are are a stupid boy '
print
("在s中找stupid:"
,s.find(
'stupid'))
print
("字串are的統計:"
,s.count(
'are'))
print
("字串are的替換(are):"
,s.replace(
'are'
,'are'))
print
("去字串中的左右空格:"
,s.strip())
defstrsplit()
:print
("字串中的分割和組合\n"
) s =
'how time flies'
print
("字串分割:"
,s.split())
print
("字串組合:"
,'!'
.join(
['oh'
,'ya'
,'xi'])
)def
strtest()
:print
("字串測試\n"
) s =
'abcdefg'
print
("測試是否全是字母:"
,s.isalpha())
print
("測試是否全是數字:"
,s.isdigit())
print
("測試是否全是空白字元:"
,s.isspace())
print
("測試是否全是小寫:"
,s.islower())
print
("測試是否全是大寫:"
,s.isupper())
print
("測試首字母是不是大寫:"
,s.istitle())
if __name__ ==
'__main__'
: strcase(
) strfind(
) strsplit(
) strtest(
)
Python 字串操作方法
1.capitalize 把字串的第乙個字元改為大寫 2.casefold 把整個字串的所有字元改寫小寫 3.center width 將字串居中,並使用空格填充至長度width的新字串 4.count sub start end 返回sub在字串裡面出現的次數,start和end引數表示範圍,可選...
python 字串操作方法
字串物件的操作方法 序列操作方法 內建函式或表示式,如lenth 和型別特定方法 物件方法呼叫,如s.find 說明 模式 pattern 匹配是指正規表示式,re模組。而文字串分隔符就是簡單的字串。字串分割 str.split python內建函式,返回值為列表,只能傳入單一的文字串分隔符,如st...
python字串操作方法
一 hello world hello oworld s1 hello world s3 hell oworld print s1,s3,end 二 hello world hello,world s1 hello world s2 n hello,world n print s1,s2,end 三...