python 문자열 사용 방법 요약

  • types
    >>> x=1
    >>> type(x) is types.IntType
    True
    >>> y='aa'
    >>> type(y) is types.StringType
    True
    >>> z=[]
    >>> type(z) is types.ListType
    True
    >>> a={}
    >>> type(a) is types.DictType
    True
  • 문자열 의 조작
    >>> "a"+"b"
    'ab'
    >>> "a"*4
    'aaaa'
    >>> "-".join(['a','b','c'])
    'a-b-c'
    >>> "".join(['a','b','c'])
    'abc'
    >>> 'a,b,c'.split(',')
    ['a', 'b', 'c']
    >>> 'a-b-c'.split('-')
    ['a', 'b', 'c']
    >>> aa="""fan
    ... te
    ... fei"""
    >>> aa.splitlines()                  #    ,      
    ['fan', 'te', 'fei']
    >>> print ''.join(aa.splitlines())
    fantefei
    >>> print ''.join(aa.splitlines(True))#    ,    True ,     
    fan
    te
    fei
    >>> "abcde".startswith('ab'),"abcde".endswith('de') #        
    (True, True)
    >>> "abc".upper(),"ABC".lower() #           
    ('ABC', 'abc')
    >>> "abcabc".find('bc') #          bc   
    1
    >>> "abcabc".find('bc',2)#          bc   
    4
    >>> "abcabc".find('bcd')  #           -1
    -1
    >>> "abcabc".index('bc')  #index    find    
    1
    >>> "abcabc".index('bc',2)
    4
    >>> "abcabc".index('bcd')  #   index             
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: substring not found
    >>> "abcabc".rindex('bc')  #     bc        
    4
    >>> "abcabc".rindex('bc')  #     bc        
    4
    >>> "#say you hello yaaaaass#".strip('#') #     #,             、\r、
    'say you hello yaaaaass' >>> "#say you hello yaaaaass#".lstrip('#') # 'say you hello yaaaaass#' >>> "#say you hello yaaaaass#".rstrip('#') # '#say you hello yaaaaass' >>> "#say you hello yaaaaass#".strip('#say') # ['#','s','a','y'] , ' you hello ' >>> "#say you hello yaaaaass#".strip('#say ') # ['#','s','a','y',' '] , 'ou hello' >>> "#saiy you hello yaaaaass#".strip('#say ') # i 'iy you hello' :lstrip、rstrip strip >>> "abcabcabc".replace('bc','BC') #replace , 'aBCaBCaBC' >>> "abcabcabc".replace('bc','BC',1) # 'aBCabcabc' >>> "abcabcabc".replace('bc','BC',2) # 2 'aBCaBCabc' >>> '123'.ljust(5,'0') #5 , , 0 '12300' >>> '123'.rjust(5,'0') #5 , , 0 '00123' >>> '123'.center(5,'*') #5 , , * '*123*' >>> '123'.center(2,'*') # (2) , '123' >>> '123'.zfill(5) #zfill , 0 '00123' >>> '123'.zfill(2) '123'
  • 좋은 웹페이지 즐겨찾기