Python文字列の一般的な操作のまとめ

4038 ワード

Pythonでよく見られる文字列の処理方法について説明します
1、文字列接続
# 1、      +
>>> 'hello'+' world'
'hello world'
# 2、  
>>> "%s %s" %('hello',' world')
'hello  world'
# 3、join  
>>> ' '.join(['hello','world'])
'hello world'

2、文字列回転配列
a = 'My name is Jason'
#  split(str="", num=string.count(str))            ,        ,    ' '.join    
>>> 'My name is Jason'.split(' ')
['My', 'name', 'is', 'Jason']
>>> ' '.join(['My', 'name', 'is', 'Jason'])
'My name is Jason'

3、文字列比較
>>> 'hello' == 'hello'
True
>>> 'hello' != 'hello'
False
>>> '123' is 123
False
>>> '123' is not 123
True
# python2      cmp,python3      
>>> import operator
>>> operator.eq('world',' world')
False

4、文字列検索
#     
>>> ml = 'Machine Learning'
>>> 'Le' in ml
True
>>> ml.find('n')
5
>>> ml.rfind('n')
14
>>> ml.index('n')
5
>>> ml.rindex('n')
14
>>> ml.find('N')
-1
>>> ml.index('N')
Traceback (most recent call last):
  File "", line 1, in 
    ml.index('N')
ValueError: substring not found

5、文字列置換
#           
>>> '123hello'.replace('123','')
'hello'
#    
>>> import re
>>> re.sub("[0-9]",'','123hello')
'hello'

6、文字列の先頭と末尾の一致
>>> 'cat.jpg'.startswith('cat')
True
>>> 'cat.jpg'.startswith('cat',0,3)
True
>>> 'cat.jpg'.endswith('.jpg')
True
>>> 'cat.jpg'.endswith('.jpg',-4)
True

7、文字列スペース処理
>>> s = '  Hello World   '
>>> s.strip()
'Hello World'
>>> s.lstrip()
'Hello World   '
>>> s.rstrip()
'  Hello World'
#  
>>> 'www.example.com'.lstrip('www.')
'example.com'
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'

8、文字列のフォーマット、数字と大文字と小文字の判断、長さの補完
#       
>>> '{name},{sex},{age}'.format(age=15,sex='male',name='  ')
'  ,male,15'
>>> '{1},{0},{2}'.format('15','  ','male')
'  ,15,male'
>>> '{},{},{}'.format('  ', '15','male')
'  ,15,male'

#               ,         ,    ,     
>>> '123'.isdigit()
True
>>> '123   '.isdigit()
False
#isnumeric               
>>> '123   '.isnumeric()
True

#          
>>> 'abc'.islower()
True
>>> 'Abc'.islower()
False
>>> 'ABC'.isupper()
True

#     
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
#      
>>> import re
>>> def titlecase(s):
...     return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
...                   lambda mo: mo.group(0)[0].upper() +
...                              mo.group(0)[1:].lower(),
...                   s)
...
>>> titlecase("they're bill's friends.")
"They're Bill's Friends."

#         ,   0,   csv    00     0    
>>> code = '1'
>>> code.zfill(6)
'000001'

#        
>>> s = '   '
>>> len(s)
3
>>> for i in s:
    print(i)
 
 
 
>>>