python文字列の基本操作

19722 ワード

note
  • アクセス:ループ、インデックス、スライス
  • フォーマットアクセスformat
  • 接合+
  • find,index,replace
  • の置換と検索
  • 統計len,count
  • 大文字と小文字title,upper,lower
  • スペースと改行文字strip
  • 分割split
  • 判定タイプisalpha,isdigit,isalnum,isidentifier
  • 符号化encodeと復号decode
  • dirは、すべての操作可能な方法
  • をリストする.
    アクセス:ループ、インデックス、スライス
    >>> s = 'abcdefg'
    >>> s[-2]
    'f'
    >>> s[0::2]
    'aceg'
    >>> s[0::]
    'abcdefg'
    >>> s[::-1]
    'gfedcba'
    

    フォーマットアクセスformat
    {}で占拠し、format()で記入する
    print("I {} excellent!".format("am"))
    url = 'https://www.{}.net/'
    name = 'csdn'
    whole_url = url.format(name)
    print(url,"
    "
    , whole_url) ''' : I am excellent! https://www.{}.net/ https://www.csdn.net/ '''

    結合+
    a = 'I am'
    b = 'a boy.'
    c = a + ' ' + b
    >>> c
    'I am a boy.'
    

    find,index,replaceの置換と検索
    findは、エレメントの最初の位置を見つけたインデックス値を返します.戻り-1 indexが見つかりません.放出異常が見つかりません.
    >>> url = 'https://www.{}.net/'.format('csdn')
    >>> url.find('csdn')
    12
    >>> url.index('csdn')
    12
    >>> url.find('csadn')
    -1
    >>> url.index('csadn')
    Traceback (most recent call last):
      File "", line 1, in <module>
    ValueError: substring not found
    
    >>> new_url = url.replace('csdn', 'cs')
    >>> new_url
    'https://www.cs.net/'
    >>> url
    'https://www.csdn.net/'
    

    統計len,count
    countは、ある文字(列)が文字列に現れる回数を統計します.
    >>> url = 'https://www.{}.net/'.format('csdn')
    >>>len(url)
    21
    >>> url.count('w')
    3
    >>> url.count('ww')
    1
    >>> url.count('www')
    1
    

    大文字と小文字title,upper,lower
    >>> str = 'i am a boY.'
    >>> str.title()
    'I Am A Boy.'
    >>> str.upper()
    'I AM A BOY.'
    >>> str.lower()
    'i am a boy.'
    

    スペースと改行文字を削除strip
    strip()メソッドは、文字列の先頭と末尾に指定された文字(デフォルトではスペースまたは改行)または文字シーケンスを削除するために使用されます.注意:この方法では、先頭または末尾の文字しか削除できません.中間部分の文字は削除できません.
    >>> str = ' i am a boY.
    '
    >>> str.strip() 'i am a boY.' >>> str.lstrip() 'i am a boY.
    '
    >>> str.rstrip() ' i am a boY.'

    スプリットsplit
    >>> str = ' i am a boY.
    '
    >>> str.split(' ') ['', 'i', 'am', 'a', 'boY.
    '
    ]

    判定タイプisalpha,isdigit,isalnum,isidentifier
    isidentifier:識別子かどうかを判断する
    符号化encodeと復号decode
    https://www.cnblogs.com/shine-lee/p/4504559.html
    dirはすべての操作可能な方法をリストします
    >>>str='abc'
    >>> dir(str)
    ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']