(原創)Python字符串系列(1)——str對象


  在本博客 《Python字符串系列》 中,將介紹以下內容:

  1. Python內置的str對象及操作
  2. 字符串的格式化
  3. Python中的正則表達式
  4. re模塊

 

  本文將介紹Python內置的 str 類型,列舉Python中字符串對象支持的方法,使用這些方法可以實現強大的字符串處理功能。

  在Python 2 中,普通字符串與Unicode字符串有着明確的區分,二者都是Python內置的基本類型,例如:

>>> type(str)
<type 'type'>
>>> type(unicode)
<type 'type'>

  str 類型的字符串通常被稱為普通字符串(plain string),區別於 unicode 類型的字符串(unicode string):

>>> s = 'this is a plain string.'
>>> type(s)
<type 'str'>
>>> u = u'this is a unicode string.'
>>> type(u)
<type 'unicode'>

  unicode字符串通常以 'u' 開頭。

  在Python 2中,使用 抽象基類 basestring 判斷一個字符串是不是 str 類型或者 unicode 類型的實例,因為二者都是 basestring 的子類。

>>> issubclass(str, basestring)
True
>>> issubclass(unicode, basestring)
True
>>> issubclass(unicode, str)
False

  本文將介紹Python字符串的內置方法,這些方法對於 plain string 和 unicode 字符串同樣適用,如果執行操作的s是一個plain string,那么返回的字符串也是一個plain string,unicode類似。后續的文章將詳細分析 unicode 字符串的特性及編碼上的一些特點。

  Python中,字符串是不可變的序列,支持:重復、連接、索引和切片等操作,例如:

>>> s * n    # 將 s 重復n次
>>> s1 + s2    # 將字符串 s1 和 s2 連接
>>> s[0]    # 索引字符串 s1 中的第一個字符
>>> s[0:2]    # 返回字符串 s 中的前兩個字符

  這些操作都不會改變參與運算的字符串,除非進行顯式地賦值。此外,Python還包括了許多字符串處理的小技巧,如:

  • 使用s[::-1]可以翻轉整個字符串
  • 如果一個字符串全部由數字組成,而開頭有0,則使用 int(s) 能夠自動除去開頭的0,將原來的字符串轉成一個有意義的整數。

  

Python內置了豐富的字符串處理功能

1. 首字母大寫

capitalize()

s.capitalize()

  返回s的一份拷貝,並不改變s。如果 s 的首字符是一個字母,則拷貝的首字母將其改成大寫,其余所有字母轉成小寫。

例如:

>>> 'this is a test string.'.capitalize()
'This is a test string.'
>>> '_this is a test string.'.capitalize()# 開頭不是字母,不變
'_this is a test string.'
>>> 'this is A test string.'.capitalize()# 除開頭外的其他位置上的字母全轉成小寫
'This is a test string.'

  

2. 對齊方式

(1)左右對齊  ljust()、rjust()

s.ljust(width[, fillchar])
s.rjust(width[, fillchar])

  返回一個長度為 max(len(s), width) 的字符串,如果 width > len(s),則左/右對齊,並在另一端填充 fillchar

例如:

>>> '1234'.rjust(8, '#')
'####1234'
>>> '1234'.ljust(8, '#')
'1234####'
>>> '1234'.ljust(2, '#')
'1234'

(2)居中  center()

s.center(n, fillchar=' ')

  返回一個新的字符串,新字符串的長度為 max(len(s), n),當 n > len(s)時,使用參數 fillchar (默認為空格)填充新字符串中其余的位置,並將 s 置於新字符串的中部。

例如:

>>> 'test'.center(3)
'test'
>>> 'test'.center(5)
' test'
>>> 'test'.center(6, '#')
'#test#'
>>> 'test'.center(7, '~')
'~~test~'

  可見當左右無法均衡填充時,優先填充左側。

 

3. 計數

count()

s.count(sub, start=0, end=sys.maxint)

  統計 s[start:end] 中,子串 sub 出現的次數。

 

4. str 與 unicode 的轉換

(1)str到unicode——decode()

S.decode([encoding[,errors]])

   使用 decode() 函數可以將 str 類型的plain string 轉換成 unicode 類型的字符串,

例如:

>>> s = '你好'
>>> u = s.decode('gbk')
>>> type(s)
<type 'str'>
>>> type(u)
<type 'unicode'>
>>> print s
你好
>>> print u
你好
>>> s
'\xc4\xe3\xba\xc3'
>>> u
u'\u4f60\u597d'
>>> len(s)
4
>>> len(u)
2

  

(2)Unicode 到 str——encode() 

S.encode([encoding[,errors]])

  使用encode()則可以將 unicode 字符串 轉換成 str 類型的 plain string。

例如:

>>> u = u'你好'
>>> s = u.encode('gbk')
>>> type(u)
<type 'unicode'>
>>> type(s)
<type 'str'>
>>> u
u'\u4f60\u597d'
>>> s
'\xc4\xe3\xba\xc3'
>>> print u
你好
>>> print s
你好
>>> len(u)
2
>>> len(s)
4

 

5. 前后綴

endswith()、startswith()

S.endswith(suffix[, start[, end]])
s.startswith(prefix[, start[, end]])

  返回 bool 型結果,

  判斷 s[start:end]是否以 suffix 結尾。

 

6. 擴展制表符

expandtabs()

S.expandtabs([tabsize])

  tabsize默認為8,返回一個 s 的拷貝,其中的“\t”都被擴展成 tabsize 個空格。

例如:

>>> 'test\ttest'.expandtabs()
'test    test'

  

7. 定位

(1)定位不到時返回 -1  find()、rfind()

s.find(sub [,start [,end]])
s.rfind(sub [,start [,end]])

  返回 int 型結果,表示 sub 在 s[start:end] 中第一次出現的下標。如果在 s[start:end] 中沒有找到子串 sub,則返回 -1。

例如:

>>> 'testtest'.find('est')
1
>>> 'testtest'.find('tt')
3
>>> 'testtest'.find('tt',3)
3
>>> 'testtest'.find('tt',4)
-1

 

(2)定位不到時拋出異常  index()、rindex()

S.index(sub [,start [,end]])
s.rindex(sub [,start [,end]])

  功能與 find() 類似,但是如果沒有找到 sub ,則拋出 ValueError。

例如:

>>> 'hello this world'.index('$')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

  

8.

format()

S.format(*args, **kwargs)

  返回 字符串 型結果,

 

9. 內容形式判斷

isalnum()、isalpha()、isdigit()、isspace()

s.isalnum()

  返回布爾型結果,

  判斷 s 是不是非空,且全部由 字母 和 數字 組成。

 

s.isalpha()

  返回布爾型結果,

  判斷 s 是不是非空,且全部由 字母 組成。

 

s.isdigit()

  返回布爾型結果,

  判斷 s 是不是非空,且全部由 數字 字符組成。

例如:

>>> '123'.isdigit()
True
>>> '123.456'.isdigit()
False

  

s.isspace()

  如果 len(s) > 0,且其中的所有字符都是空格,則返回True;

  如果 s 為空,或s中存在至少一個非空格的字符,則返回False。

 

10. 大小寫

(1)小寫  islower()、lower()

s.islower()
s.lower()

  返回布爾型結果,

  如果 s 中不含一個小寫字母,或至少含有一個大寫字母,則返回False,否則返回True,包含其他字符並不影響。

例如:

>>> '123.456'.islower()
False
>>> 'abcde'.islower()
True
>>> 'abcde$'.islower()
True
>>> 'abcde#%^%'.islower()
True
>>> 'abcdeF'.islower()
False
>>> 'a.213214$#@^%$@'.islower()
True

 

(2)大寫  isupper()、upper()

s.isupper()
s.upper()

   如果 s 中包含的所有字母都是大寫,則返回 True

  如果s 中不包含字母,或者至少包含一個小寫字母,則返回False。

例: 

>>> 'ABC$@'.isupper()
True
>>> 'ASDFGq'.isupper()
False
>>> ''.isupper()
False

  

(3)交換大小寫  swapcase()

s.swapcase()

  

11. "titlecase"

istitle()title()

s.istitle()
s.title()

  判斷一個字符串是不是“titlecase”:每個獨立的連續字母段都以大寫字母開頭。

例如:

>>> 'A Title'.istitle()
True
>>> 'a Title'.istitle()
False
>>> '123 this is a string'.istitle()
False
>>> 'This Is a String'.istitle()
False

  

12. 連接

join()

s.join(iterable)

  以 s 為分隔符連接 iterable 中的字符串

例如:

>>> '$'.join(['hello','this','world'])
'hello$this$world'

  

13. 拆分

(1)保留分隔符的一次拆分  partition()rpartition()

s.partition(sep)
s.rpartition(sep)

  以 sep 為分隔符拆分 s ,返回 (head, sep, tail) 形式的三元組。

例如:

>>> 'hello$this$world'.partition('$')
('hello', '$', 'this$world')
>>> 'hello$this$world'.rpartition('$')
('hello$this', '$', 'world')
>>> 'hello this world'.partition('$')
('', '', 'hello this world')

  

(2)不保留分隔符的完全拆分  split()rsplit()splitlines()

s.split([chars])
s.rsplit([sep [,maxsplit]])
s.splitlines(keepends=False)

例如:

>>> 'hello$this$world'.split('$')
['hello', 'this', 'world']

  

14. 

lstrip()、strip()rstrip()

s.lstrip([chars]) #從開頭刪除
s.strip([chars]) # 左右兩端同時刪除
s.rstrip([chars]) # 從結尾刪除

  

16. 替換

replace()

s.replace(old, new[, count])

 

18.

translate()

s.translate(table [,deletechars])

  

19.
zfill()
s.zfill(width)

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM