ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 파이썬 내장 함수
    개발노트 2020. 2. 4. 15:01
    if __name__ == "__main__":
        #리스트의 깊은 복사
        mylist = [1,2,3,4]
        newlist = mylist[:]
        newlist2 = list(mylist)
    
        print(newlist)
        print(newlist2)
    
        #set의 깊은 복사
        print("----set 복사 ----")
        people = {"A", "B", "C"}
        slayers = people.copy() #딕셔너리 카피
        slayers.discard("C") # "C" 삭제
        slayers.discard("B") # "B" 삭제
        print(slayers)
        print(people)
    
        #기타 객체의 깊은 복사를 할 때는 copy 모듈 사용
        import copy
        mystr = "테스트"
        newstr = copy.copy(mystr) #얕은 복사
        newstr2 = copy.deepcopy(mystr) #깊은 복사
        print("----copy 모듈 사용 ----")
        print(newstr)
        print(newstr2)
    
    
        print("----join  사용 ----")
        mystr1 = ['유튜브', '구글', '네이버']
        mystr2 = ' '.join(mystr1)
        print(mystr2)
        mystr2 = '-<>-'.join(mystr1)
        print(mystr2)
        mystr2 = ''.join(mystr1)
        print(mystr2)
    
        print("---- ljust(), rjust() 사용 ----")
        name = '아이유'
        re_name = name.ljust(50, '-')
        print(re_name)
        re_name = name.rjust(50, '-')
        print(re_name)
    
        print("---- format() 사용 ----")
        print("{0} {1}".format("안녕", "파이썬"))
        print("이름: {who}, 나이: {age}".format(who="me", age=25))
        print("이름: {who}, 나이: {0}".format(15, who="me"))
        print("{} {} {}".format("파이썬", "자료구조", "알고리즘"))
        name = '아이유'
        num = 999
        print("{num}: {name}".format(**locals()))
    
        print("---- splitlines() 사용 ----")
        names = "아이유\n청하"
        print(names.splitlines())
    
        print("---- split() 사용 ----")
        split_str = '아이유*청하-제키와이*999'
        get_split = split_str.split("*")
        print(get_split)
        get_split = get_split[1].split("-")
        print(get_split)
    
        print("---- split() 사용해서 모든 공백 제거하기 ----")
        def erase_space_from_string(string):
            s1 = string.split(" ")
            s2 = "".join(s1)
            return print(s2)
        erase_space_from_string("가 나 다 라")
    
        print("---- split() 문자열 분리하기 ----")
        start = '안녕*파이썬*!'
        print(start.split("*", 1))
        print(start.rsplit("*", 1))
    
        print("---- strip() 문자열 제거 ----")
        mystr = '안녕 & 파이썬222'
        print(mystr.strip("222"))
    
        print("---- swapcase() 대소문자를 반전 ----")
        mystr = 'abcd EFG'
        print(mystr.swapcase())
    
        print("---- index(), find() 메서드 ----")
        mystr = '가나다 라마바'
        print(mystr.find('라'))
        print(mystr.index('라'))
    
        print("---- count() 메서드 ----")
        mystr = '파이썬 메서드 파이썬테스트 파이썬'
        print(mystr.count("파이썬", 0, -1))
        print(mystr.count("파이썬"))
    
        print("---- replace() 메서드 ----")
        mystr = '파이썬 메서드 파이썬테스트 파이썬'
        print(mystr.replace("파이썬", "python", 2))
    
        print("---- f-strings ----")
        a = 123
        b = '가나다'
        print(f'숫자{a} 문자열{b}')

     

    출력된 결과
    C:\Users\jinho\PycharmProjects\untitled\venv\Scripts\python.exe "C:/Users/jinho/PycharmProjects/untitled/깊은 복사와 슬라이싱 연산.py"
    [1, 2, 3, 4]
    [1, 2, 3, 4]
    ----set 복사 ----
    {'A'}
    {'C', 'A', 'B'}
    ----copy 모듈 사용 ----
    테스트
    테스트
    ----join  사용 ----
    유튜브 구글 네이버
    유튜브-<>-구글-<>-네이버
    유튜브구글네이버
    ---- ljust(), rjust() 사용 ----
    아이유-----------------------------------------------
    -----------------------------------------------아이유
    ---- format() 사용 ----
    안녕 파이썬
    이름: me, 나이: 25
    이름: me, 나이: 15
    파이썬 자료구조 알고리즘
    999: 아이유
    ---- splitlines() 사용 ----
    ['아이유', '청하']
    ---- split() 사용 ----
    ['아이유', '청하-제키와이', '999']
    ['청하', '제키와이']
    ---- split() 사용해서 모든 공백 제거하기 ----
    가나다라
    ---- split() 문자열 분리하기 ----
    ['안녕', '파이썬*!']
    ['안녕*파이썬', '!']
    ---- strip() 문자열 제거 ----
    안녕 & 파이썬
    ---- swapcase() 대소문자를 반전 ----
    ABCD efg
    ---- index(), find() 메서드 ----
    4
    4
    ---- count() 메서드 ----
    2
    3
    ---- replace() 메서드 ----
    python 메서드 python테스트 파이썬
    ---- f-strings ----
    숫자123 문자열가나다
    
    

     

    댓글

Designed by Tistory.