ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • String
    python/기본 2022. 4. 24. 22:10
    # String 은 immutable
    
    print("I don't know")
    print('I don\'t know')
    print('say "I don\'t know"')  # say "I don't know"
    print(r'C:\name\file')  # C:\name\name
    
    # "-----------------" 이후에 공백 없이 line1 이 출력되게
    print("--------------------")
    print("""\
    line1
    line2
    line3\
    """)
    print("--------------------")
    
    print('Hi.' * 3 + 'Mike.')  # Hi.Hi.Hi.Mike.
    
    s = 'aaaaaaaaaaaaaa' \
        'bbbbbbbbbbbbbb'
    print(s)  # aaaaaaaaaaaaaabbbbbbbbbbbbbb
    
    word = '012345'
    print(word[0])  # 0
    print(word[1])  # 1
    print(word[-1])  # 5
    print(word[0:2])  # 01
    print(word[:2])  # 01
    print(word[2:])  # 2345
    print(word[2:5])  # 234
    print(word[2:-2])  # 23
    print(word[::-1])  # 543210
    
    len(word)  # 6
    
    # word[0] = 'j'  # TypeError: 'str' object does not support item assignment
    print('j' + word[1:])  # j12345
    
    s = 'My name is Mike. Hi Mike'
    print(s.startswith('My'))  # True
    print(s.find('Mike'))  # 11
    print(s.rfind('Mike'))  # 20
    print(s.replace('Mike', 'Nancy'))  # My name is Nancy. Hi Nancy
    
    'a is {}'.format('a')  # a is a
    'a is {2} {1} {0}'.format(1, 2, 3)  # a is 3 2 1
    'My name is {name} {family}'.format(name='JM', family='Kim')
    
    str(3.14)  # '3.14'
    int('1')  # 1
    str(True)  # 'True'
    
    a = 'a'
    print(f'a is {a}')
    
    x, y, z = 1, 2, 3
    print(f'a is {x}, {y}, {z}')

    'python > 기본' 카테고리의 다른 글

    Set  (0) 2022.04.29
    Dictionary  (0) 2022.04.29
    Tuple  (0) 2022.04.29
    List  (0) 2022.04.29
    변수, 수치, print 출력  (0) 2022.04.24

    댓글

Designed by Tistory.