Python-字符串類型

字符串類型str======================================基本使用======================================一、用途:記錄描述性值的狀態,好比名字、性別等二、定義方式msg='hello world' #msg=str('hello world')類型轉換: 能夠把任意類型專場字符串類型res1=str(10)res2=str(10.3)res3=str([1,2,3])res4=str({'x':1}) #res4="{'x':1}"print(type(res1))print(type(res2))print(type(res3))print(type(res4))三、經常使用操做+內置的方法優先掌握的操做:(*****)一、按索引取值(正向取+反向取) :只能取msg='hello world'print(type(msg[0]))print(msg[-1])msg[0]='H'二、切片(顧頭不顧尾,步長)msg='hello world'print(msg[0]+msg[1]+msg[2])print(msg[0:5])print(msg[0:5:2]) #0 2 4print(msg[0:]) #print(msg[:]) #print(msg[-1:-5:-1]) #-1 -2 -3 -4print(msg[::-1]) #-1 -2 -3 -4三、長度len:統計的是字符的個數msg='h你d'print(len(msg))四、成員運算in和not in:判斷一個子字符串是否存在與一個大字符串中msg='hello world'print('ho' in msg)print('ho' not in msg)五、移除空白strip:移除字符串左右兩邊的某些字符msg='      hello      'print(msg.strip(' '))print(msg.strip())print(msg)name=input('name>>>: ').strip() #name='egon'pwd=input('password>>>: ').strip()if name == 'egon' and pwd == '123':    print('login successfull')else:    print('username or password error')msg='***h**ello**********'print(msg.strip('*'))msg='*-=+h/ello*(_+__'print(msg.strip('*-=+/(_'))六、切分split: 把有規律的字符串切成列表從而方便取值info='egon:18:180:150'res=info.split(':',1)print(res)print(res[1])info='egon:18:180:150'res=info.split(':')print(res)s1=res[0]+':'+res[1]+':'+res[2]+':'+res[3]s1=''for item in res:    s1+=itemprint(s1)s1=':'.join(res)print(s1)':'.join([1,2,3,4,5])七、循環for i in 'hello':    print(i)須要掌握的操做(****)一、strip,lstrip,rstripmsg='*****hello****'print(msg.strip('*'))print(msg.lstrip('*'))print(msg.rstrip('*'))二、lower,uppermsg='AaBbCc123123123'print(msg.lower())print(msg.upper())三、startswith,endswithmsg='alex is dsb'print(msg.startswith('alex'))print(msg.endswith('sb'))四、format的三種玩法msg='my name is %s my age is %s' %('egon',18)print(msg)msg='my name is {name} my age is {age}'.format(age=18,name='egon')print(msg)瞭解msg='my name is {} my age is {}'.format(18,'egon')msg='my name is {0}{0} my age is {1}{1}{1}'.format(18,'egon')print(msg)五、split,rsplitcmd='get|a.txt|33333'print(cmd.split('|',1))print(cmd.rsplit('|',1))六、replacemsg='kevin is sb kevin kevin'print(msg.replace('kevin','sb',2))七、isdigit #當字符串內爲純數字時結果爲Trueres='11111'print(res.isdigit())int(res)age_of_bk=18inp_age=input('your age: ').strip()if inp_age.isdigit():    inp_age=int(inp_age) #int('asdfasdfadfasdf')    if inp_age > 18:        print('too big')    elif inp_age < 18:        print('to small')    else:        print('you got it')else:    print('必須輸入純數字')瞭解(**)一、find,rfind,index,rindex,countprint('xxxkevin is sb kevin'.find('kevin'))print('xxxkevin is sb kevin'.index('kevin'))print('xxxkevin is sb kevin'.rfind('kevin'))print('xxxkevin is sb kevin'.rindex('kevin'))res='xxxkevin is sb kevin'.find('kevasdfsadfin')print(res)res='xxxkevin is sb kevin'.index('kevasdfsadfin')print('kevin is kevin is kevin is sb'.count('kevin'))二、center,ljust,rjust,zfillprint('egon'.center(50,'*'))print('egon'.ljust(50,'*'))print('egon'.rjust(50,'*'))print('egon'.zfill(50))三、captalize,swapcase,titleprint('my name is kevin'.capitalize())print('AaBbCc'.swapcase())print('my name is kevin'.title())四、is其餘name='egon123'print(name.isalnum()) #字符串由字母或數字組成print(name.isalpha()) #字符串只由字母組成print(name.islower())print(name.isupper())name='    'print(name.isspace())msg='I Am Egon'print(msg.istitle())======================================該類型總結====================================存一個值有序不可變(一、可變:值變,id不變。可變==不可hash 二、不可變:值變,id就變。不可變==可hash)x='aaa'print(id(x))x='bbb'print(id(x))
相關文章
相關標籤/搜索