欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂Python培訓(xùn)之Python3字符串操作總結(jié)

2019-07-24 14:14:33 5579瀏覽

今天千鋒扣丁學(xué)堂Python培訓(xùn)老師給大家分享一篇關(guān)于python3字符串操作總結(jié)的詳細(xì)介紹,中通過(guò)示例代碼介紹的非常詳細(xì),下面我們一起來(lái)看一下吧。



字符串截取

>>>s = 'hello'
>>>s[0:3]
'he'
>>>s[:] #截取全部字符
'hello'

消除空格及特殊符號(hào)

s.strip() #消除字符串s左右兩邊的空白字符(包括'\t','\n','\r','')
s.strip('0') #消除字符串s左右兩邊的特殊字符(如'0'),字符串中間的'0'不會(huì)刪除

例如:

>>>s = '000hello00world000'
>>>s.strip('0')
'hello00world'
s.strip('12')等價(jià)于s.strip('21')

例如:

>>>s = '12hello21'
>>>s.strip('12')
'hello'

lstrip,rstrip用法與strip類似,分別用于消除左、右的字符

字符串復(fù)制

s1 = 'hello'
s2 = s1 # s2 = 'hello'

若指定長(zhǎng)度

s1 = 'hello'
s2 = s1[0:2] #s2 = 'he'

字符串連接

s1 = 'hello'
s2 = 'world'
s3 = s1 + s2 #s3 = 'helloworld'

或者

import operator
s3 = operator.concat(s1,s2) #concat為字符串拼接函數(shù)

字符串比較

(1)利用operator模塊方法比較(python3.X取消了cmd函數(shù))

包含的方法有:

lt(a,b)————小于

le(a,b)————小于等于

eq(a,b)————等于

ne(a,b)————不等于

ge(a,b)————大于等于

gt(a,b)————大于

例子:

>>>import operator
>>>operator.eq('abc','edf') #根據(jù)ASCII碼比較
Flase
>>>operator.gt('abc','ab')
True

(2)關(guān)系運(yùn)算符比較(>,<,>=,<=,==,!=)

>>>s1 = 'abc'
>>>s2 = 'ab'
>>>s1 > s2
True
>>>s1 == s2
False

求字符串長(zhǎng)度

>>>s1 = 'hello'
>>>len(s1)
5

求字符串中最大字符,最小字符

>>>s1 = 'hello'
>>>max(s1) #求字符串s1中最大字符
'o'
>>>min(s1) #求字符串s2中最小字符
'e'

字符串大小寫(xiě)轉(zhuǎn)換

主要有如下方法:

upper————轉(zhuǎn)換為大寫(xiě)

lower————轉(zhuǎn)換為小寫(xiě)

title————轉(zhuǎn)換為標(biāo)題(每個(gè)單詞首字母大寫(xiě))

capitalize————首字母大寫(xiě)

swapcase————大寫(xiě)變小寫(xiě),小寫(xiě)變大寫(xiě)

例子:

>>>s1 = 'hello'
>>>s2 = 'WORLD'
>>>s3 = 'hello world'
>>>s1.upper() 
'HELLO'
>>>s2.lower() 
'world'
>>>s3.title() 
'Hello World'
>>>s3.capitalize() 
'Hello world'
>>>s3.title().swapcase() 
'hELLO wORLD'

字符串翻轉(zhuǎn)

>>>s1 = 'hello'
>>>s1[::-1]
'olleh'

字符串分割

split方法,根據(jù)參數(shù)進(jìn)行分割,返回一個(gè)列表

例子:

>>>s1 = 'hello,world'
>>>s1.split(',')
['hello','world']

字符串序列連接

join方法:

語(yǔ)法為str.join(seq)#seq為元素序列

例子:

>>>l = ['hello','world']
>>>str = '-'
>>>str.join(l)
'hello-world'

字符串內(nèi)查找

find方法:

檢測(cè)字符串內(nèi)是否包含子串str

語(yǔ)法為:

str.find(str[,start,end])#str為要查找的字符串;strat為查找起始位置,默認(rèn)為0;end為查找終止位置,默認(rèn)為字符串長(zhǎng)度。若找到返回起始位置索引,否則返回-1

例子:

>>>s1 = 'today is a fine day'
>>>s1.find('is')
6
>>>s1.find('is',3)
6
>>>s1.find('is',7,10)
-1

字符串內(nèi)替換

replace方法:

把字符串中的舊串替換成新串

語(yǔ)法為:

str.replace(old,new[,max]) #old為舊串,new為新串,max可選,為替換次數(shù)

例子:

>>>s1 = 'today is a find day'
>>>s1.replace('find','rainy')
'today is a rainy day'

判斷字符串組成

主要有如下方法:

isdigit————檢測(cè)字符串時(shí)候只由數(shù)字組成

isalnum————檢測(cè)字符串是否只由數(shù)字和字母組成

isalpha————檢測(cè)字符串是否只由字母組成

islower————檢測(cè)字符串是否只含有小寫(xiě)字母

isupper————檢測(cè)字符串是否只含有大寫(xiě)字母

isspace————檢測(cè)字符串是否只含有空格

istitle————檢測(cè)字符串是否是標(biāo)題(每個(gè)單詞首字母大寫(xiě))

例子:

>>>s1 = 'hello'
>>>s1.islower()
True
>>>s1.isdigit()
False

字符串轉(zhuǎn)數(shù)組

a = 'My name is Jason'
#使用split(str="", num=string.count(str)) 方法根據(jù)不同的分割符轉(zhuǎn),也可指定分割次數(shù),可使用 ' '.join方法轉(zhuǎn)回
>>> 'My name is Jason'.split(' ')
['My', 'name', 'is', 'Jason']
>>> ' '.join(['My', 'name', 'is', 'Jason'])
'My name is Jason'

字符串首尾匹配

>>> 'cat.jpg'.startswith('cat')
True
>>> 'cat.jpg'.startswith('cat',0,3)
True
>>> 'cat.jpg'.endswith('.jpg')
True
>>> 'cat.jpg'.endswith('.jpg',-4)
True

字符串空格處理

>>> s = ' Hello World  '
>>> s.strip()
'Hello World'
>>> s.lstrip()
'Hello World  '
>>> s.rstrip()
' Hello World'
#擴(kuò)展
>>> 'www.example.com'.lstrip('www.')
'example.com'
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'


字符串格式化、數(shù)字及大小寫(xiě)判斷、長(zhǎng)度補(bǔ)全

#字符串的格式化
>>> '{name},{sex},{age}'.format(age=15,sex='male',name='小安')
'小安,male,15'
>>> '{1},{0},{2}'.format('15','小安','male')
'小安,15,male'
>>> '{},{},{}'.format('小安', '15','male')
'小安,15,male'
 
#如果字符串中的所有字符都是數(shù)字,并且至少有一個(gè)字符,則返回真,否則返回假
>>> '123'.isdigit()
True
>>> '123一二三'.isdigit()
False
#isnumeric 是所有字符都是數(shù)字字符返回真
>>> '123一二三'.isnumeric()
True
 
#字符串是否大小寫(xiě)判斷
>>> 'abc'.islower()
True
>>> 'Abc'.islower()
False
>>> 'ABC'.isupper()
True
 
#首字母大寫(xiě)
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
#正則處理方式
>>> import re
>>> def titlecase(s):
...   return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
...          lambda mo: mo.group(0)[0].upper() +
...               mo.group(0)[1:].lower(),
...          s)
...
>>> titlecase("they're bill's friends.")
"They're Bill's Friends."
 
#返回指定長(zhǎng)度字符串,前面補(bǔ)0,一般存csv文件中含00開(kāi)頭的字符0會(huì)被抹掉
>>> code = '1'
>>> code.zfill(6)
'000001'
 
#字符串長(zhǎng)度及遍歷
>>> s = '扣丁'
>>> len(s)
3
>>> for i in s:
  print(i)
扣
丁
>>>

以上就是關(guān)于扣丁學(xué)堂Python培訓(xùn)之Python3字符串操作總結(jié)的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,想要了解更多關(guān)于Python和人工智能方面內(nèi)容的小伙伴,請(qǐng)關(guān)注扣丁學(xué)堂Python培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺(tái)為您提供權(quán)威的Python開(kāi)發(fā)環(huán)境搭建視頻,Python培訓(xùn)后的前景無(wú)限,行業(yè)薪資和未來(lái)的發(fā)展會(huì)越來(lái)越好的,扣丁學(xué)堂老師精心推出的Python視頻教程定能讓你快速掌握Python從入門到精通開(kāi)發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂Python技術(shù)交流群:279521237。


扣丁學(xué)堂微信公眾號(hào)                          Python全棧開(kāi)發(fā)爬蟲(chóng)人工智能機(jī)器學(xué)習(xí)數(shù)據(jù)分析免費(fèi)公開(kāi)課直播間


      【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】         【掃碼進(jìn)入Python全棧開(kāi)發(fā)免費(fèi)公開(kāi)課】



查看更多關(guān)于"Python開(kāi)發(fā)資訊"的相關(guān)文章>

標(biāo)簽: Python培訓(xùn) Python視頻教程 Python在線視頻 Python學(xué)習(xí)視頻 Python培訓(xùn)班

熱門專區(qū)

暫無(wú)熱門資訊

課程推薦

微信
微博
15311698296

全國(guó)免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠(chéng)科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部