Chapter03-2 문자형

    728x90

    #Chapter03-2
    # 파이썬 문자형
    # 문자형 중요

    #문자열 생성
    str1="I am Python"
    str2='python'
    str3="""how are you?"""
    str4='''thank you!'''
    print(type(str1),type(str2),type(str3),type(str4))
    print(len(str1),len(str2),len(str3),len(str4))

    #빈 문자열
    str1_t1 = ''
    str2_t2 = str()

    print(type(str1_t1),len(str1_t1))
    print(type(str2_t2),len(str2_t2))

    #이스케이프문 문자 사용
    #I'm Boy

    print("I'm Boy")
    print('I\'m Boy')
    print('I\\m Boy')

    print('a \t b')
    print('a \n b')
    print('a \"\" b')

    escape_str1 = "Do you have a \"retro games\""
    print(escape_str1)
    escape_str2 = 'what\'s on TV?'
    print(escape_str2)

    #탭, 줄바꿈
    t_s1 = "Click \t Start!"
    t_s2 = "New Line \n Check!"
    print(t_s1)
    print(t_s2)

    #Raw String
    raw_s1 = r'D:\python\test'
    print(raw_s1)

    #멀티라인 입력
    #역슬래쉬 사용
    multi_str = \
    '''
    String
    Multi line
    test
    '''
    print(multi_str)

    #문자열 연산
    str_o1 = "Python"
    str_o2 = "Apple"
    str_o3 = "How are you doing"
    str_o4 = "Seoul, Daejeon, Busan, Jinju"

    print(str_o1 *3)
    print(str_o1 + str_o2)
    print('y' in str_o1)
    print('z' in str_o1)
    print('P' not in str_o2)

    #문자열 형 변환
    print(str(66))
    print(str(True))
    print(str(10.1))
    #문자열 함 (upper, isalnum, startswith, count, endwith, isalpha...)
    print("Capitalize: ", str_o1.capitalize()) # 첫글자 대문자로 출력
    print("endswith?: ", str_o2.endswith("e")) #마지막 글자 확인
    print("replace", str_o1.replace("thon",'Good')) #문자 대체
    print("sorted:", sorted(str_o1)) #문자 리스트로 정렬
    print("split:", str_o4.split(','))#,기준으로 나눔


    #반복 (시퀀스)
    im_str="Good Boy!"
    print(dir(im_str)) #__iter__

    #출력
    for i in im_str:
        print(i)

    #슬라이싱
    str_s1 = "Nice Python"

    #슬라이싱 연습
    print(str_s1[0:3]) #0 1 2
    print(str_s1[5:])
    print(str_s1[:len(str_s1)]) #str_s1[:11]
    print(str_s1[:len(str_s1)-1]) #str_s1[:10]
    print(str_s1[1:9:2]) #1:9 = 범위 2 = 단위
    print(str_s1[-5:])
    print(str_s1[1:-2])
    print(str_s1[::2])
    print(str_s1[::-1])

    #아스키 코드
    a = 'z'

    print(ord(a))
    print(chr(122))

    'Python > 문법' 카테고리의 다른 글

    Chapter03-6 집합  (0) 2021.12.26
    Chapter03-5 딕셔너리  (0) 2021.12.26
    Chapter03-4 튜플  (0) 2021.12.26
    Chapter03-3 리스트  (0) 2021.12.26
    Chapter03-1 숫자형  (0) 2021.12.26

    댓글