1. 程式人生 > >python基礎之字串內建函式(部分)

python基礎之字串內建函式(部分)

# -*- coding: utf-8 -*- """ @file:strstudy.py @time:2018/4/2 21:45 @author:xq @contact:[email protected] """ #把字串的第一個字元大寫 # a = 'test' # print(a.capitalize()) #'Test' #原字串居中,並使用空格填充至長度 width 的新字串 # a='center' # print(a.center(10)) # 'center' #返回 str string 裡面出現的次數,如果 beg 或者 end 指定則返回指定範圍內 str 出現的次數
# a='hello' # print(a.count('l')) #2 #檢查字串是否以 obj 結束,如果beg 或者 end 指定則檢查指定的範圍內是否以 obj 結束,如果是,返回 True,否則返回 False. # a='mytest' # print(a.endswith('t')) # print(a.endswith('m')) #True #False #如果 string 至少有一個字元並且所有字元都是字母則返回 True,否則返回 False # a= 'test' # b='12test' # c='a!' # print(a.isalpha()) # print(b.isalpha())
# print(c.isalpha()) #True #False #False #如果 string 至少有一個字元並且所有字元都是字母或數字則返回 True,否則返回 False # a= 'test' # b='12test' # c='a!' # print(a.isalnum()) # print(b.isalnum()) # print(c.isalnum()) #True #True #False #如果 string 只包含十進位制數字則返回 True 否則返回 False. # a='123' # b='12a' # c='12!' # print(a.isdecimal())
# print(b.isdecimal()) # print(c.isdecimal()) #True #False #False #如果 string 只包含數字則返回 True 否則返回 False. # a='123' # b='12a' # c='12!' # print(a.isdigit()) # print(b.isdigit()) # print(c.isdigit()) #True #False #False #如果 string 中包含至少一個區分大小寫的字元,並且所有這些(區分大小寫的)字元都是小寫,則返回 True,否則返回 False # a='asd' # b='ABC' # c='ABd' # d='123' # print(a.islower()) # print(b.islower()) # print(c.islower()) # print(d.islower()) # True # False # False # False #如果 string 中只包含空格,則返回 True,否則返回 False. # a= 'hello world' # b=' ' # print(a.isspace()) # print(b.isspace()) # False # True #如果 string 中包含至少一個區分大小寫的字元,並且所有這些(區分大小寫的)字元都是大寫,則返回 True,否則返回 False # a='AGBF' # b='Af' # print(a.isupper()) # print(b.isupper()) # True # False # string 作為分隔符,將 seq 中所有的元素(的字串表示)合併為一個新的字串 # a='*' # myseq=['1','3','5','6'] # print(a.join(myseq)) # '1*3*5*6' #返回一個原字串左對齊,並使用空格填充至長度 width 的新字串 # a='left' # print(a.ljust(10)) # 'left ' #轉換 string 中所有大寫字元為小寫. # a='MyTEs' # print(a.lower()) # 'mytes' #截掉 string 左邊的空格 # a=' hello ' # print(a.lstrip()) # 'hello ' #返回字串 str 中最大的字母 # a='test' # print(max(a)) # 't' #返回字串 str 中最小的字母 # a='test' # print(min(a)) # 'e' # str 出現的第一個位置起,把 字 符 串 string 分 成 一 個 3 元 素 的 元 組 (string_pre_str,str,string_post_str), # 如果 string 中不包含str string_pre_str == string. # a = "http://www.w3cschool.cc/" # print(a.partition("://")) # ('http', '://', 'www.w3cschool.cc/') # string 中的 str1 替換成 str2,如果 num 指定,則替換不超過 num . # a='hello world this is a test ' # print(a.replace(' ','%20')) # 'hello%20world%20this%20is%20a%20test%20' #類似於 find()函式,不過是從右邊開始查詢. # a='test' # print(a.find('t')) # print(a.rfind('t')) #0 # 3 #類似於 index(),不過是從右邊開始. # a='test' # print(a.rindex('t')) # 3 # 刪除 string 字串末尾的空格. # a=' hello ' # print(a.rstrip()) # ' hello' # str 為分隔符切片 string,如果 num有指定值,則僅分隔 num 個子字串 # a='test,test1,test2' # print(a.split(',')) # ['test', 'test1', 'test2'] #檢查字串是否是以 obj 開頭,是則返回 True,否則返回 False。如果beg end 指定值,則在指定範圍內檢查. # a='test' # print(a.startswith('t')) # True # string 上執行 lstrip() rstrip() # a=' hello ' # print(a.strip()) # 'hello' #翻轉 string 中的大小寫 # a='MytesT' # print(a.swapcase()) # 'mYTESt' # 如果 string 是標題化的( title())則返回 True,否則返回 False # a='The Name' # b='The name' # print(a.istitle()) # print(b.istitle()) # True # False #返回"標題化" string,就是說所有單詞都是以大寫開始,其餘字母均為小寫( istitle()) # a='hello world' # print(a.title()) # 'Hello World' #轉換 string 中的小寫字母為大寫 # a='test' # print(a.upper()) # 'TEST' #返回長度為 width 的字串,原字串 string 右對齊,前面填充0 # a='test' # print(a.zfill(10)) # '000000test'