pythonのstring文字列プロパティメソッド操作

5795 ワード

共通文字列
>>> import string
>>> string.ascii_letters #     
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_uppercase #    
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase #    
'abcdefghijklmnopqrstuvwxyz'
>>> string.digits # 0-9  
'0123456789'
>>> string.hexdigits #      
'0123456789abcdefABCDEF'
>>> string.octdigits #    
'01234567'
>>> string.punctuation #      
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> string.printable #       ASCII         
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t
\r\x0b\x0c' >>> string.whitespace # ASCII 。 、 、 、 、 ' \t
\r\x0b\x0c' >>>

文字列フォーマット出力位置合わせ
>>> str = "Python stRING" 
 
>>> print str.center(20)       #  20     ,str   
   Python stRING   
    
>>> print str.ljust(20)            #  20     ,str   
Python stRING      
 
>>> print str.rjust(20)            #  20     ,str   
       Python stRING

2、大文字と小文字の変換
>>> str = "Python stRING"
 
>>> str.upper()                #   
'PYTHON STRING'
 
>>> str.lower()                #    
'python string'
 
>>> str.capitalize()           #       ,    
'Python string'
 
>>> str.swapcase()         #      
'pYTHON STring'
 
>>> str.title()                #       ,      ,     
'Python String'

3、文字列条件判断
>>> str = '01234'
 
>>> str.isalnum()                #         ,        
True
>>> str.isdigit()                #      ,        
True      
 
 
>>> str = 'string'
 
>>> str.isalnum()                  #         ,        
True
>>> str.isalpha()                  #      ,         
True
>>> str.islower()                  #      ,            ,    True
True
 
>>> str = "01234abcd"
 
>>> str.islower()                  #      ,            ,    True
True
 
>>> str.isalnum()                  #         ,        
True
 
>>> str = ' '
>>> str.isspace()                  #        ,        
True
 
>>> str = 'ABC'
 
>>> str.isupper()                  #      ,            ,    True
True
 
>>> str = 'Aaa Bbb'
 
>>> str.istitle()                  #          ,   
True
 
 
>>> str = 'string learn'
 
>>> str.startswith('str')                 #      'str'  
True
 
>>> str.endswith('arn')                      #      'arn'  
True

4、文字列検索位置と置換
>>> str='string lEARn'
 
>>> str.find('z')              #     ,     -1,               
-1
 
>>> str.find('n')              #             
4
 
>>> str.rfind('n')         #             
11
 
>>> str.index('a')         #          
Traceback (most recent call last):
  File "", line 1, in 
ValueError: substring not found
  
>>> str.index("n")      # find  ,           
4
 
>>> str.rindex("n")         #            
11
 
>>> str.count('a')      #         
0
>>> str.count('n')      #  
2
 
>>> str.replace('EAR','ear')        #    
'string learn'
 
>>> str.replace('n','N')
'striNg lEARN'
 
>>> str.replace('n','N',1)
'striNg lEARn'
 
>>> str.strip('n')          #            ,            
   
'string lEAR' 
   
>>> str.lstrip('n')        #    
   
'string lEARn' 
   
>>> str.rstrip('n')        #    
   
'string lEAR' 
 
>>> str = " tab"
 
>>> str.expandtabs()       #        
' tab'
 
>>> str.expandtabs(2)      #     
' tab'

5、文字列の符号化と復号
>>> str = "     "
>>> str
'\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe5\xad\xa6\xe4\xb9\xa0'
 
>>> str.decode('utf-8')                                #    , utf-8   unicode
u'\u5b57\u7b26\u4e32\u5b66\u4e60'
 
>>> str.decode("utf-8").encode('gbk')                      #    , unicode   gbk
'\xd7\xd6\xb7\xfb\xb4\xae\xd1\xa7\xcf\xb0'
 
>>> str.decode('utf-8').encode('utf-8')                        # unicode   utf-8 
'\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe5\xad\xa6\xe4\xb9\xa0'

6、文字列分割変換
>> str = "Learn string"
 
>>> '-'.join(str)
'L-e-a-r-n- -s-t-r-i-n-g'
 
>>> li = ['Learn','string']
 
>>> '-'.join(li)
'Learn-string'
 
>>> str.split('n')
['Lear', ' stri', 'g']
 
>>> str.split('n',1)
['Lear', ' string']
 
>>> str.rsplit('n')
['Lear', ' stri', 'g']
 
>>> str.rsplit('n',1)
['Learn stri', 'g']
 
>>> str.splitlines()
['Learn string']
 
>>> str.partition('n')
('Lear', 'n', ' string')
 
>>> str.rpartition('n')
('Learn stri', 'n', 'g')