日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python-5-字符串方法

發布時間:2025/4/16 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python-5-字符串方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
center >>> "The Middle by Jimmy Eat World".center(39) ' ?? ?The Middle by Jimmy Eat World ?? ?' >>> "The Middle by Jimmy Eat World".center(39, "*") '*****The Middle by Jimmy Eat World*****' find 在字符串中查找子串,找到返回子串第一個字符索引,否則返回- >>> 'With a moo-moo here, and a moo-moo there'.find('moo') 7 >>> title = "Monty Python's Flying Circus" >>> title = "Monty Python's Flying Circus" >>> title.find('Monty') 0 >>> title.find('Zirquss') -1 成員資格檢查in智能用于單個字符,而這個可以多個 可以指定起點和終點 >>> subject = '$$$ Get rich now!!! $$$' >>> subject.find('$$$') 0 >>> subject.find('$$$', 1) # 只指定了起點 20 >>> subject.find('!!!') 16 >>> subject.find('!!!', 0, 16) # 同時指定了起點和終點 -1 起點和終點值指定的范圍包含起點不包含終點,這是python的慣用做法 join 與split相反 >>> seq = [1, 2, 3, 4, 5] >>> sep = '+' >>> sep.join(seq) # 嘗試合并一個數字列表 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: sequence item 0: expected string, int found >>> seq = ['1', '2', '3', '4', '5'] >>> sep.join(seq) # 合并一個字符串列表 '1+2+3+4+5' >>> dirs = '', 'usr', 'bin', 'env' >>> '/'.join(dirs) '/usr/bin/env' >>> print('C:' + '\\'.join(dirs)) C:\usr\bin\env lower 返回小寫版本 >>> 'Trondheim Hammer Dance'.lower() 'trondheim hammer dance' title 所有單詞首字母大寫 >>> "that's all folks".title() "That'S All, Folks" replace 將指定子串都替換為另一個字符串,并返回替換后的結果 >>> 'This is a test'.replace('is', 'eez') 'Theez eez a test' split 與join相反,將字符串拆分為序列 >>> '1+2+3+4+5'.split('+') ['1', '2', '3', '4', '5'] >>> '/usr/bin/env'.split('/') ['', 'usr', 'bin', 'env'] >>> 'Using the default'.split() ['Using', 'the', 'default'] strip 將字符串的開頭和末尾的空白刪除,并返回刪除后的結果 >>> ' internal whitespace is kept '.strip() 'internal whitespace is kept' translate 使用前要先創建一個轉換表 >>> table = str.maketrans('cs', 'kz') 兩個參數為兩個長度相同的字符串,指定將第一個字符串中的每個字符都替換為第二個字符串中相應的字符 內部存儲為unicode >>> table {115: 122, 99: 107} >>> 'this is an incredible test'.translate(table) 'thiz iz an inkredible tezt' 第三個參數為指定要將那些字母刪除 >>> table = str.maketrans('cs', 'kz', ' ') >>> 'this is an incredible test'.translate(table) 'thizizaninkredibletezt' 判斷字符串是否滿足特定條件 是true,否則false isalnum、 isalpha、 isdecimal、 isdigit、 isidentifier、 islower、 isnumeric、isprintable、 isspace、 istitle、 isupper

轉載于:https://www.cnblogs.com/swefii/p/10795504.html

總結

以上是生活随笔為你收集整理的Python-5-字符串方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。