標籤:orm and 技術分享 ndt cap 不能 nic apc world
字串資料出現的意義
掌握字串的定義和特性
能熟練掌握字串常用操作,並瞭解其他Factory 方法
字串的定義和建立
字串是一個有序的字元集合,用於儲存和表示基本的文本資訊, 用引號“ ” 之間的內容就是字串。
建立方式
字串 = “ 用引號引起來就行 ”
特性:
按照從左至右的順序定義字元集合,下表從0開始順序訪問,有序
提示:
1.字串單引號和雙引號都無法取消特殊字元的含義,假如需要取消其特殊意義:
則在其前面加上 r 如:name = r‘I\thf‘
2. unicode字串與 r 連用必須在 r 前面,如 name = ur‘I\thf‘
常用操作:
#索引
name = ‘yan xia ting yu‘
print(name[0])
>>:y
print(name[-1])
>>:u
print(name[-2])
>>:y
str.index(element)#尋找元素 假如存在 則返回索引 ,不存在報錯。
print(name.index(‘e‘))
riase拋出異常:ValueError: substring not found
str.find(element)
print(name.find(‘e‘))#元素存在返回 索引,不存在 返回-1
不會拋出異常報錯
print(name.find(‘y‘))
print(name.strip())#移除兩邊的空格
print(name.lstrip())#移除左邊的空格
print(name.rstrip())#移除右邊空格
name = ‘****** yan xia ting yu *********‘
print(name.strip("*"))#移除兩邊的*字元
#字串的長度
print(name.__len__())#一般不用,是len()方法的實現方式
>>:35
print(len(name))
>>:35
name = ‘****** yan xia ting yu *********‘
#字串的替換
print(name.replace(‘*‘,‘8‘))#不指定則全部替換
>>:888888 yan xia ting yu 888888888
print(name.replace(‘*‘,‘8‘,8))#指定替換個數
>>:888888 yan xia ting yu 88*******
print(name.replace(‘*‘,‘8‘,-8))
>>;888888 yan xia ting yu 888888888
print(name.replace(‘*‘,‘8‘,-1))
>>:888888 yan xia ting yu 888888888
#切片
name = ‘yanxiatingyu‘
print(name[0:])
print(name[0::])
print(name[0:7])#顧頭不顧尾
print(name[0:7:2])#步長為2
print(name[4::2])#步長為2
print(name[::-1])#反向步長 #取反
print(name[:-7:-1])
print(name[-2:-7:-1])
print(name[-1:-7:-1])
>>:yanxiatingyu
>>:yanxiatingyu
>>:yanxiat
>>:ynit
>>:itny
>>:uygnitaixnay
>>:uygnit
>>:ygnit
>>:uygnit
#一些比較有用的函數
name = ‘yanxiatingyuyanxiatingyu‘
upper_name=‘YANXIATINGYU‘
print(name.capitalize())
print(upper_name.casefold())
print(name.center(20,‘8‘))#第一個參數設定寬度,第二個:補全的字元:
print(name.center(20))#不寫預設為空白格#原來的字串置中,不夠用字元補全
print(name.count(‘y‘,0,7))#統計字元出現的次數,start_int_index,end_int_index設定尋找範圍
print(name.count(‘y‘))#不設定不限制:預設範圍是全部
Yanxiatingyuyanxiatingyu
yanxiatingyu
yanxiatingyuyanxiatingyu
yanxiatingyuyanxiatingyu
1
4
import hashlib
hs=hashlib.md5()
hs.update(name.encode(‘utf-8‘))
print(hs.hexdigest())
>>:ecbf46328d68cca2a29e618186efd377
#字串的格式輸出
#format的三種玩法res=‘{} {} {}‘.format(‘egon‘,18,‘male‘)res=‘{1} {0} {1}‘.format(‘egon‘,18,‘male‘)res=‘{name} {age} {sex}‘.format(sex=‘male‘,name=‘egon‘,age=18)
#字串的拼接
單獨拿出來了 有興趣可以去看看
# in 和 not in 成員運算
if ‘y‘ in name:
print(‘存在‘)
if ‘Y‘ not in name:
print(‘不存在‘)
#1、strip,lstrip,rstrip#2、lower,upper#3、startswith,endswith#4、format的三種玩法#5、split,rsplit#6、join#7、replace#8、isdigit
#stripname=‘*egon**‘print(name.strip(‘*‘))print(name.lstrip(‘*‘))print(name.rstrip(‘*‘))#lower,uppername=‘egon‘print(name.lower())print(name.upper())#startswith,endswithname=‘alex_SB‘print(name.endswith(‘SB‘))print(name.startswith(‘alex‘))#splitname=‘root:x:0:0::/root:/bin/bash‘print(name.split(‘:‘)) #預設分隔符號為空白格name=‘C:/a/b/c/d.txt‘ #只想拿到頂級目錄print(name.split(‘/‘,1))name=‘a|b|c‘print(name.rsplit(‘|‘,1)) #從右開始切分
>>:[‘a|b‘, ‘c‘]
#join tag=‘ ‘ print(tag.join([‘egon‘,‘say‘,‘hello‘,‘world‘]))
#可迭代對象必須都是字串
#replace name=‘yanxiatingyu say :i have ,my name is yanxiatingyu‘
print(name.replace(‘yanxiatingyu‘,‘88‘,1))
#isdigit:可以判斷bytes和unicode類型,是最常用的用于于判斷字元是否為"數字"的方法
age=input(‘>>: ‘)
print(age.isdigit())
print(name.capitalize())
#首字母大寫
print(name.swapcase())
#全部大寫
print(name.istitle())
#判斷首字母是否大寫
#find,rfind,index,rindex,countname=‘egon say hello‘print(name.find(‘o‘,1,3)) #顧頭不顧尾,找不到則返回-1不會報錯,找到了則顯示索引# print(name.index(‘e‘,2,4)) #同上,但是找不到會報錯print(name.count(‘e‘,1,3)) #顧頭不顧尾,如果不指定範圍則尋找所有#center,ljust,rjust,zfillname=‘egon‘print(name.center(30,‘-‘))print(name.ljust(30,‘*‘))print(name.rjust(30,‘*‘))print(name.zfill(50)) #用0填充#expandtabsname=‘egon\thello‘print(name)print(name.expandtabs(1))#captalize,swapcase,titleprint(name.capitalize()) #首字母大寫print(name.swapcase()) #大小寫翻轉msg=‘egon say hi‘print(msg.title()) #每個單詞的首字母大寫#is數字系列#在python3中num1=b‘4‘ #bytesnum2=u‘4‘ #unicode,python3中無需加u就是unicodenum3=‘四‘ #中文數字num4=‘Ⅳ‘ #羅馬數字#isdigt:bytes,unicodeprint(num1.isdigit()) #Trueprint(num2.isdigit()) #Trueprint(num3.isdigit()) #Falseprint(num4.isdigit()) #False#isdecimal:uncicode#bytes類型無isdecimal方法print(num2.isdecimal()) #Trueprint(num3.isdecimal()) #Falseprint(num4.isdecimal()) #False#isnumberic:unicode,中文數字,羅馬數字#bytes類型無isnumberic方法print(num2.isnumeric()) #Trueprint(num3.isnumeric()) #Trueprint(num4.isnumeric()) #True#三者不能判斷浮點數num5=‘4.3‘print(num5.isdigit())print(num5.isdecimal())print(num5.isnumeric())‘‘‘總結: 最常用的是isdigit,可以判斷bytes和unicode類型,這也是最常見的數字應用情境 如果要判斷中文數字或羅馬數字,則需要用到isnumeric‘‘‘#is其他print(‘===>‘)name=‘egon123‘print(name.isalnum()) #字串由字母或數字組成print(name.isalpha()) #字串只由字母組成print(name.isidentifier())print(name.islower())print(name.isupper())print(name.isspace())print(name.istitle())
python 基礎資料型別 (Elementary Data Type) 之 字串