最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python字符串常用方法

 更新時(shí)間:2018年06月14日 09:29:47   作者:爬呀爬Xjm  
這篇文章主要介紹了python字符串常用方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1、 isalnum() :判斷字符串所有的字符都是字母或者數(shù)字。返回true和false

In [1]: str1='jiangwei520'
In [2]: str2='jiang wei'
In [3]: str3='520'
In [4]: str4='520 1314'
In [5]: str1.isalnum()
Out[5]: True
In [6]: str2.isalnum()
Out[6]: False
In [7]: str3.isalnum()
Out[7]: True
In [8]: str4.isalnum()
Out[8]: False

2、 isalpha() :判斷字符串所有的字符都是字母。返回true和false

In [11]: s1='j w'
In [12]: s2='jw'
In [13]: s1.isalpha()
Out[13]: False
In [14]: s2.isalpha()
Out[14]: True

3、 isdigit(): 判斷字符串所有的字符都是數(shù)字。返回true和false

In [15]: n1='12 34'
In [16]: n2='1234'
In [17]: n3='1.1'
In [18]: n1.isdigit()
Out[18]: False
In [19]: n2.isdigit()
Out[19]: True
In [20]: n3.isdigit()
Out[20]: False

4、 islower() :判斷所有的字符都是小寫。

In [23]: s1='j w'
In [24]: s2='jw'
In [25]: s3='JW'
In [26]: s1.islower()
Out[26]: True
In [27]: s2.islower()
Out[27]: True
In [28]: s3.islower()
Out[28]: False

5、 isupper() :判斷所有的字符都是大寫。

In [29]: s1='J w'
In [30]: s2="J W"
In [31]: s3="JW"
In [32]: s4='Jw'
In [33]: s1.isupper()
Out[33]: False
In [34]: s2.isupper()
Out[34]: True
In [35]: s3.isupper()
Out[35]: True
In [36]: s4.isupper()
Out[36]: False

6、 istitle() :判斷每個(gè)單詞的首字母都是大寫。

In [37]: s1='hello world'
In [38]: s2='Hello World'
In [39]: s3='Hello,world'
In [40]: s4='HELLO WORLD'
In [41]: s1.istitle()
Out[41]: False
In [42]: s2.istitle()
Out[42]: True
In [43]: s3.istitle()
Out[43]: False
In [44]: s4.istitle()
Out[44]: False

7、 lower() :轉(zhuǎn)小寫

In [47]: s4
Out[47]: 'HELLO WORLD'
In [48]: s4.lower()
Out[48]: 'hello world'
In [49]: s2
Out[49]: 'Hello World'
In [50]: s2.lower()
Out[50]: 'hello world'

7、 upper() :轉(zhuǎn)大寫

In [54]: s1
Out[54]: 'HEllo WOrld'
In [55]: s3
Out[55]: 'Hello,world'
In [56]: s1.upper()
Out[56]: 'HELLO WORLD'
In [57]: s3.upper()
Out[57]: 'HELLO,WORLD'

8、 strip([chars]) :去除

lstrip()和 rstrip() 類似
In [61]: s1='  hello     world   !!!  '
In [62]: s1.strip()
Out[62]: 'hello     world   !!!'
In [63]: s2='**** jw---love---you ****'
In [64]: s2.strip('*')
Out[64]: ' jw---love---you '
#應(yīng)該是去除兩邊的
 In [107]: a='***111***'
 In [108]: a.lstrip('*')
 Out[108]: '111***'
 In [109]: a.rstrip('*')
 Out[109]: '***111'
 In [110]: a.strip('*')
 Out[110]: '111'

9、 replace(old ,new, [count]) :替換

In [72]: a='小喵和小九'
In [73]: a.replace('喵','喵喵').replace('九','九九')
Out[73]: '小喵喵和小九九'
In [74]: b='jiangwei is a good good good boy'
In [75]: b.replace('good','nice')
Out[75]: 'jiangwei is a nice nice nice boy'
In [76]: b.replace('good','nice',2)
Out[76]: 'jiangwei is a nice nice good boy'
In [77]: b.replace('good','nice',1)
Out[77]: 'jiangwei is a nice good good boy'

10、 split() :切割。返回列表。

In [92]: path1
Out[92]: 'a/b/c/d'
In [93]: path1.split('/')
Out[93]: ['a', 'b', 'c', 'd']
In [88]: path='/home/centos/python3.6'
In [89]: path
Out[89]: '/home/centos/python3.6'
In [90]: path.split('/')
Out[90]: ['', 'home', 'centos', 'python3.6']

11、 startswith() :以指定的字符串開頭。發(fā)貨true和false。

endswith():類似
In [94]: a='helloworld'
In [95]: b='hello world'
In [96]: a.startswith('hello')
Out[96]: True
In [97]: b.startswith('hello')
Out[97]: True
In [98]: b.startswith('he')
Out[98]: True

12、 format() :格式化輸出

In [111]: print('{name}---->{age}'.format(name='xjm',age=21))
xjm---->21
In [112]:

13、 title() : 把每個(gè)字符串的首字母大寫

In [112]: s='hello world python '
In [113]: s.title()
Out[113]: 'Hello World Python '
#與capitalize不同。就第一個(gè)單詞的首字母大寫
 In [128]: a='hello world python'
In [129]: a.capitalize()
Out[129]: 'Hello world python'

14、 join() :插入

In [117]: a='.'
In [118]: a.join('jwlove')
Out[118]: 'j.w.l.o.v.e'
 In [124]: a='/'
In [125]: b=('user','local') 
In [127]: a.join(b)
Out[127]: 'user/local'

15、 center(width,char) :擴(kuò)充

In [137]: a='Linux'
In [138]: a.center(25,'*')
Out[138]: '**********Linux**********'
In [140]: a.center(25)
Out[140]: '     Linux    '
#ljust和rjust類似
 In [142]: a.ljust(10,'-')
 Out[142]: 'Linux-----'
 In [143]: a.rjust(10,'-')
 Out[143]: '-----Linux'

16、 splitlines(): 根據(jù)\r  \n  \r\n  切割。返回列表

In [151]: a='如果\n沒有\(zhòng)r如果'
In [154]: print(a)
如果
如果
In [157]: a.splitlines()
Out[157]: ['如果', '沒有', '如果']

17、 format_map() :格式化輸出

In [158]: a='hello world {course}'
In [160]: course1='python'
In [161]: course2='java'
In [178]: a.format(course=course1)
Out[178]: 'hello world java'
In [179]: a.format(course=course2)
Out[179]: 'hello world python'
In [181]: a.format_map(vars())
Out[181]: 'hello world python'

總結(jié)

以上所述是小編給大家介紹的python字符串常用方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 詳解Python如何實(shí)現(xiàn)Excel數(shù)據(jù)讀取和寫入

    詳解Python如何實(shí)現(xiàn)Excel數(shù)據(jù)讀取和寫入

    這篇文章主要為大家詳細(xì)介紹了python如何實(shí)現(xiàn)對(duì)EXCEL數(shù)據(jù)進(jìn)行讀取和寫入,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Python中schedule擴(kuò)展的具體使用

    Python中schedule擴(kuò)展的具體使用

    Python的schedule模塊是一個(gè)輕量級(jí)的Python庫,用于在指定時(shí)間執(zhí)行某些操作,本文就來介紹一下Python中schedule擴(kuò)展的具體使用,感興趣的可以了解一下
    2024-12-12
  • 簡(jiǎn)單了解Python3里的一些新特性

    簡(jiǎn)單了解Python3里的一些新特性

    這篇文章主要介紹了簡(jiǎn)單了解Python3里的一些新特性,給大家總結(jié)一下Python3一些新的更方便的特性!希望你們看完后也能高效率的編寫代碼,需要的朋友可以參考下
    2019-07-07
  • python實(shí)現(xiàn)數(shù)據(jù)挖掘中分箱的示例代碼

    python實(shí)現(xiàn)數(shù)據(jù)挖掘中分箱的示例代碼

    數(shù)據(jù)分箱(英語:Data?binning)是一種數(shù)據(jù)預(yù)處理方法,用于最大限度地減少小觀測(cè)誤差的影響,本文主要為大家介紹了python實(shí)現(xiàn)數(shù)據(jù)分箱的相關(guān)知識(shí),感興趣的可以了解下
    2024-01-01
  • Python技巧分享之如何將字符串轉(zhuǎn)回DataFrame格式

    Python技巧分享之如何將字符串轉(zhuǎn)回DataFrame格式

    平常我們使用pandas,一般使用的是DataFrame和Series,但個(gè)別交換數(shù)據(jù)的時(shí)候,只能使用字符串,那如何再將字符串再轉(zhuǎn)回DataFrame格式呢,本文就來和大家講講解決辦法
    2023-06-06
  • Django日志模塊logging的配置詳解

    Django日志模塊logging的配置詳解

    日志在程序開發(fā)中是少不了的,通過日志我們可以分析到錯(cuò)誤在什么地方,有什么異常。在生產(chǎn)環(huán)境下有很大的用處。在java 開發(fā)中通常用 log4j,logback 等三方組件。下面這篇文章主要介紹了Django日志模塊logging的相關(guān)資料,需要的朋友可以參考下。
    2017-02-02
  • python?動(dòng)態(tài)規(guī)劃問題解析(背包問題和最長(zhǎng)公共子串)

    python?動(dòng)態(tài)規(guī)劃問題解析(背包問題和最長(zhǎng)公共子串)

    這篇文章主要介紹了python?動(dòng)態(tài)規(guī)劃(背包問題和最長(zhǎng)公共子串),在動(dòng)態(tài)規(guī)劃中,你要將某個(gè)指標(biāo)最大化。在這個(gè)例子中,你要找出兩個(gè)單詞的最長(zhǎng)公共子串。fish和fosh都包含的最長(zhǎng)子串是什么呢,感興趣的朋友跟隨小編一起看看吧
    2022-05-05
  • Python基礎(chǔ)總結(jié)之itertools模塊詳解

    Python基礎(chǔ)總結(jié)之itertools模塊詳解

    itertools模塊是Python中一個(gè)鮮為人知但功能強(qiáng)大的工具,它專注于高效、內(nèi)存友好的迭代器操作,使其成為處理大型或復(fù)雜數(shù)據(jù)集的理想選擇,今天我們一起探討Python標(biāo)準(zhǔn)庫中的一個(gè)隱藏的寶藏:itertools模塊,感興趣的朋友一起看看吧
    2024-06-06
  • Python遍歷文件夾和讀寫文件的實(shí)現(xiàn)方法

    Python遍歷文件夾和讀寫文件的實(shí)現(xiàn)方法

    本篇文章主要介紹了Python遍歷文件夾和讀寫文件的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Python 3中print函數(shù)的使用方法總結(jié)

    Python 3中print函數(shù)的使用方法總結(jié)

    這篇文章主要給大家總結(jié)介紹了關(guān)于Python 3中print函數(shù)的使用方法,python3中的print函數(shù)和之前版本的用法相差很多,本文通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-08-08

最新評(píng)論

沿河| 昌都县| 卓资县| 广元市| 竹山县| 云和县| 进贤县| 西华县| 乐昌市| 玉山县| 宁乡县| 福安市| 庐江县| 东丽区| 潮州市| 广昌县| 建平县| 英德市| 大丰市| 廊坊市| 阳西县| 磐安县| 尼勒克县| 台南县| 卫辉市| 青龙| 威海市| 五大连池市| 平潭县| 镇坪县| 铜鼓县| 子长县| 巴彦淖尔市| 正阳县| 象州县| 浪卡子县| 东至县| 项城市| 金乡县| 三台县| 临武县|