2-07 문자열 유형 설명

24977 단어
데이터 유형 - 문자열
문자열은 질서정연한 문자 집합으로 기본적인 텍스트 정보를 저장하고 나타내는 데 사용되며, 한 쌍의 단일, 더블 또는 세 인용부호 사이에 포함된 내용을 문자열이라고 부른다.
생성:
 
s = 'Hello,beauty!How are you?'

 
특징:
  • 순서
  • 변경 불가
  • 자주 사용: isdigit replace find count index split strip center format join
    s = 'Hello World!'
    
    #
    print(s.swapcase())
    
    #     
    print(s.capitalize())
    
    #      
    print(s.casefold())
    
    #             :*******************Hello World!*******************
    print(s.center(50,'*'))
    
    #       
    print(s.count('o'))    #2
    print(s.count('o',0,5)) #        1
    
    #              
    print(s.endswith('!'))  # True
    
    #  tab , tab        
    s1 = 'a\tb'
    print(s1)     #  a    b
    print(s1.expandtabs(20)) #   a                   b
    
    #
    print(s.find('W'))   #6
    print(s.find('W',0,3))  #              -1
    
    #      
    s3 = 'my name is {0},i am {1} years old.'
    print(s3.format('Alex',22))                     # my name is Alex,i am 22 years old.
    
    s3 = 'My name is {name}, I am {age} years old.'
    print(s3.format(name='Alex',age = '22'))

     
    >>> #         
    ...
    >>> s = 'Hello World!'
    >>> s.index('o')
    4
    >>> s.index('o',5,6)
    Traceback (most recent call last):
      File "", line 1, in 
    ValueError: substring not found
    >>> #        
    ...
    >>> '22b'.isalnum()
    True
    >>> '22b*'.isalnum()
    False
    >>> #     
    ...
    >>> '222'.isalpha()
    False
    >>> 'abcc'.isalpha()
    True
    >>> '22bb'.isalpha()
    False
    >>> #     
    ...
    >>> '234'.isdecimal()
    True
    >>> '2ab4'.isdecimal()
    False
    >>> '22.22'.isdecimal()
    False
    >>>
    >>> #         
    ...
    >>> '332'.isidentifier()
    False
    >>> 'd332'.isidentifier()
    True
    >>>
    >>> #       
    ...
    >>> 'ab'.islower()
    True
    >>> 'ab3'.islower()
    True
    >>> 'abAA'.islower()
    False
    >>> #             
    ...
    >>> 'ab4'.isnumeric()
    False
    >>> '114'.isnumeric()
    True
    >>>
    >>> #       
    ...
    >>> '2bg'.isprintable()
    True
    >>> #       
    ...
    >>> ' '.isspace()
    True
    >>> 'ab'.isspace()
    False
    >>> 'a b'.isspace()
    False
    >>> #     
    ...
    >>> 'ABC'.isupper()
    True
    >>>
    >>> 'abA'.isupper()
    False

     
    >>> #                                                                                                                                                                                                 
    ...
    >>> names = ['alex','jack','rain']                                                                                                                                                             
    >>> ''.join(names)                                                                                                                                                                                    
    'alexjackrain'
    >>> ' '.join(names)
    'alex jack rain'
    >>> '*'.join(names)
    'alex*jack*rain'
    >>>
    >>> #
    ...
    >>> s = 'Hello World!'                                                                                                                                                                                
    >>> s.ljust(50)
    'Hello World!                                      '
    >>> s.ljust(50,'-')
    'Hello World!--------------------------------------'
    >>> #                                                                                                                                                                                                 
    ...
    >>> s = 'Hello World!'                                                                                                                                                                          
    >>> s.lower()
    'hello world!'
    >>> #                                                                                                                                                                                                 
    ...
    >>> s.upper()
    'HELLO WORLD!'
    >>> #       、  、tab                                                                                                                                                                                   
    ...
    >>> s = '
    hello world
    ' >>> s.lstrip() 'hello world ' >>> s.rstrip() '
    hello world
    '
    >>> #                     
    >>> str_in = 'abcdef' >>> str_out = '!@#$%^' >>> table=str.maketrans(str_in,str_out) >>> 'abc'.translate(table) '!@#'
    >>> #     
    >>> s.replace('o','-')
    'Hell- W-rld'
    >>> s.replace('o','-',1)
    'Hell- World'
    >>> #                                                                                                                                                                                                 
    ...
    >>> s = 'hello world!'                                                                                                                                                                                
    >>> s.rfind('o')
    7
    >>> s.rindex('o')
    7
    >>> #                                                                                                                                                                                                 
    ...
    >>> s = 'hello world' 
    >>> s.split()
    ['hello', 'world']
    >>> s.split('o')
    ['hell', ' w', 'rld']
    >>> s.split('o',1)
    ['hell', ' world']
    >>> s.rsplit('o',1)
    ['hello w', 'rld']
    >>> #                                                                                                                                                                                                
    ...
    >>> s = 'a
    b
    c
    dld
    ' >>> s.splitlines() ['a', 'b', 'c', 'dld']
    >>> #                                                                                                                                                                                                 
    ...
    >>> s = 'hello world'
    >>> s.startswith('he')
    True
    >>> s.startswith('hello')
    True
    >>> s.startswith('heh')
    False
    >>> s.endswith('d')
    True
    >>> #          ,    0                                                                                                                                                                                 
    ...
    >>> s = 'hello world'
    >>> s.zfill(40)
    '00000000000000000000000000000hello world'

     
    전재 대상:https://www.cnblogs.com/echo-kid-coding/p/11163281.html

    좋은 웹페이지 즐겨찾기