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

歡迎訪問 生活随笔!

生活随笔

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

python

Python内置的字符串处理函数整理

發布時間:2025/3/17 python 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python内置的字符串处理函数整理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Python內置的字符串處理函數整理 27 May 2011 16:30 Friday by 小屋 標簽: 函數 字符串 Python 字符串處理

收集常用的Python 內置的各種字符串處理 函數的使用方法

str='python String function'

生成字符串變量str='python String function'字符串長度獲取:len(str) 例:print '%s length=%d' % (str,len(str))字母處理 全部大寫:str.upper() 全部小寫:str.lower() 大小寫互換:str.swapcase() 首字母大寫,其余小寫:str.capitalize() 首字母大寫:str.title() print '%s lower=%s' % (str,str.lower()) print '%s upper=%s' % (str,str.upper()) print '%s swapcase=%s' % (str,str.swapcase()) print '%s capitalize=%s' % (str,str.capitalize()) print '%s title=%s' % (str,str.title())格式化相關 獲取固定長度,右對齊,左邊不夠用空格補齊:str.ljust(width) 獲取固定長度,左對齊,右邊不夠用空格補齊:str.ljust(width) 獲取固定長度,中間對齊,兩邊不夠用空格補齊:str.ljust(width) 獲取固定長度,右對齊,左邊不足用0補齊 print '%s ljust=%s' % (str,str.ljust(20)) print '%s rjust=%s' % (str,str.rjust(20)) print '%s center=%s' % (str,str.center(20)) print '%s zfill=%s' % (str,str.zfill(20))字符串搜索相關 搜索指定字符串,沒有返回-1:str.find('t') 指定起始位置搜索:str.find('t',start) 指定起始及結束位置搜索:str.find('t',start,end) 從右邊開始查找:str.rfind('t') 搜索到多少個指定字符串:str.count('t') 上面所有方法都可用index代替,不同的是使用index查找不到會拋異常,而find返回-1 print '%s find nono=%d' % (str,str.find('nono')) print '%s find t=%d' % (str,str.find('t')) print '%s find t from %d=%d' % (str,1,str.find('t',1)) print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2)) #print '%s index nono ' % (str,str.index('nono',1,2)) print '%s rfind t=%d' % (str,str.rfind('t')) print '%s count t=%d' % (str,str.count('t'))字符串替換相關 替換old為new:str.replace('old','new') 替換指定次數的old為new:str.replace('old','new',maxReplaceTimes) print '%s replace t to *=%s' % (str,str.replace('t', '*')) print '%s replace t to *=%s' % (str,str.replace('t', '*',1))字符串去空格及去指定字符 去兩邊空格:str.strip() 去左空格:str.lstrip() 去右空格:str.rstrip() 去兩邊字符串:str.strip('d'),相應的也有lstrip,rstrip str=' python String function ' print '%s strip=%s' % (str,str.strip()) str='python String function' print '%s strip=%s' % (str,str.strip('d'))按指定字符分割字符串為數組:str.split(' ') 默認按空格分隔 str='a b c de' print '%s strip=%s' % (str,str.split()) str='a-b-c-de' print '%s strip=%s' % (str,str.split('-'))字符串判斷相關 是否以start開頭:str.startswith('start') 是否以end結尾:str.endswith('end') 是否全為字母或數字:str.isalnum() 是否全字母:str.isalpha() 是否全數字:str.isdigit() 是否全小寫:str.islower() 是否全大寫:str.isupper() str='python String function' print '%s startwith t=%s' % (str,str.startswith('t')) print '%s endwith d=%s' % (str,str.endswith('d')) print '%s isalnum=%s' % (str,str.isalnum()) str='pythonStringfunction' print '%s isalnum=%s' % (str,str.isalnum()) print '%s isalpha=%s' % (str,str.isalpha()) print '%s isupper=%s' % (str,str.isupper()) print '%s islower=%s' % (str,str.islower()) print '%s isdigit=%s' % (str,str.isdigit()) str='3423' print '%s isdigit=%s' % (str,str.isdigit())

還有其他常見的Python字符串處理 函數的話不定期更新。

轉載于:https://my.oschina.net/u/877567/blog/199772

總結

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

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