python字串方法

來源:互聯網
上載者:User

標籤:download   start   python3   root   word   cas   dig   方法   ide   

# name=‘egon‘ #name=str(‘egon‘)
# print(type(name))


#優先掌握
#    移除空白strip
# msg=‘ hello ‘
# print(msg)
# print(msg.strip())

# msg=‘***hello*********‘
# msg=msg.strip(‘*‘)
# print(msg)

# print(msg.lstrip(‘*‘))
# print(msg.rstrip(‘*‘))

#用處
# while True:
# name=input(‘user: ‘).strip()
# password=input(‘password: ‘).strip()
#
# if name == ‘egon‘ and password == ‘123‘:
# print(‘login successfull‘)

 

#     切分split
# info=‘root:x:0:0::/root:/bin/bash‘
# print(info[0]+info[1]+info[2]+info[3])

# user_l=info.split(‘:‘)
# print(user_l[0])

# msg=‘hello world egon say hahah‘
# print(msg.split()) #預設以空格作為分隔字元

cmd=‘download|xhp.mov|3000‘
# cmd_l=cmd.split(‘|‘)
# print(cmd_l[1])
# print(cmd_l[0])

# print(cmd.split(‘|‘,1))

#用處
# while True:
# cmd=input(‘>>: ‘).strip()
# if len(cmd) == 0:continue
# cmd_l=cmd.split()
# print(‘命令是:%s 命令的參數是:%s‘ %(cmd_l[0],cmd_l[1]))

 


#
#     長度len

# print(len(‘hell 123‘))

#
#     索引

#
#     切片:切出子字串
# msg=‘hello world‘
# print(msg[1:3]) #1 2
# print(msg[1:4]) #1 2 3

 

# 掌握部分
# oldboy_age=84
# while True:
# age=input(‘>>: ‘).strip()
# if len(age) == 0:continue
# if age.isdigit():
# age=int(age)
# else:
# print(‘must be int‘)

 

 

#startswith,endswith
# name=‘alex_SB‘
# print(name.endswith(‘SB‘))
# print(name.startswith(‘alex‘))


#replace
# name=‘alex say :i have one tesla,my name is alex‘
# print(name.replace(‘alex‘,‘SB‘,1))

# print(‘my name is %s my age is %s my sex is %s‘ %(‘egon‘,18,‘male‘))
# print(‘my name is {} my age is {} my sex is {}‘.format(‘egon‘,18,‘male‘))
# print(‘my name is {0} my age is {1} my sex is {0}:{2}‘.format(‘egon‘,18,‘male‘))
# print(‘my name is {name} my age is {age} my sex is {sex}‘.format(
# sex=‘male‘,
# age=18,
# name=‘egon‘))


# name=‘goee say hello‘
# # print(name.find(‘S‘,1,3)) #顧頭不顧尾,找不到則返回-1不會報錯,找到了則顯示索引
# # print(name.index(‘S‘)) #同上,但是找不到會報錯
#
# print(name.count(‘S‘,1,5)) #顧頭不顧尾,如果不指定範圍則尋找所有


#join
# info=‘root:x:0:0::/root:/bin/bash‘
# print(info.split(‘:‘))

# l=[‘root‘, ‘x‘, ‘0‘, ‘0‘, ‘‘, ‘/root‘, ‘/bin/bash‘]
# print(‘:‘.join(l))


#lower,upper
# name=‘eGon‘
# print(name.lower())
# print(name.upper())

 

 

 

 

 

 

#瞭解部分
#expandtabs
# name=‘egon\thello‘
# print(name)
# print(name.expandtabs(1))


#center,ljust,rjust,zfill
# name=‘egon‘
# # print(name.center(30,‘-‘))
# print(name.ljust(30,‘*‘))
# print(name.rjust(30,‘*‘))
# print(name.zfill(50)) #用0填充


#captalize,swapcase,title
# name=‘eGon‘
# print(name.capitalize()) #首字母大寫,其餘部分小寫
# print(name.swapcase()) #大小寫翻轉
# msg=‘egon say hi‘
# print(msg.title()) #每個單詞的首字母大寫

 

 


#在python3中
num0=‘4‘
num1=b‘4‘ #bytes
num2=u‘4‘ #unicode,python3中無需加u就是unicode
num3=‘四‘ #中文數字
num4=‘Ⅳ‘ #羅馬數字


#isdigt:str,bytes,unicode
# print(num0.isdigit())
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())

#isdecimal:str,unicode
# num0=‘4‘
# num1=b‘4‘ #bytes
# num2=u‘4‘ #unicode,python3中無需加u就是unicode
# num3=‘四‘ #中文數字
# num4=‘Ⅳ‘ #羅馬數字
# print(num0.isdecimal())
# # print(num1.)
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())

#isnumeric:str,unicode,中文,羅馬
# num0=‘4‘
# num1=b‘4‘ #bytes
# num2=u‘4‘ #unicode,python3中無需加u就是unicode
# num3=‘四‘ #中文數字
# num4=‘Ⅳ‘ #羅馬數字
#
# print(num0.isnumeric())
# # print(num1)
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())

 


#is其他
# name=‘egon123‘
# print(name.isalnum()) #字串由字母和數字組成
# name=‘asdfasdfa sdf‘
# print(name.isalpha()) #字串只由字母組成
#

# name=‘asdfor123‘
# print(name.isidentifier())
name=‘egGon‘
print(name.islower())
# print(name.isupper())
# print(name.isspace())
name=‘Egon say‘
print(name.istitle())

python字串方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.