-
파이썬 스타일 코드
>>> colors = ['red','blue','green','yellow'] >>> result = '' >>> for s in colors: result += s >>> result 'redbluegreenyello'
▼
>>> colors = ['red','blue','green','yellow'] >>> result = ''.join(colors) >>> result 'redbluegreenyello
split( )
문자열의 값을 특정 값을 기준으로 분리하여 리스트 형태로 변환하는 방법
>>> items = 'zero one two three'.split() # 빈 칸 기준 문자열 분리 >>> items ['zero', 'one', 'two', 'three'] >>> example = 'python, java, c' >>> example.split(",") # ,를 기준으로 문자열 분리 ['python', ' java', ' c'] >>> a,b,c = example.split(",") # 분리된 각 값을 언패킹 >>> print(a,b,c) python java c
join( )
문자열로 구성된 리스트를 합쳐 하나의 문자열로 반환
>>> colors = ['red', 'blue' ,'green', 'yellow'] >>> result = ''.join(colors) >>> result 'redbluegreenyellow' >>> result = ' '.join(colors) >>> result 'red blue green yellow' >>> result = ', '.join(colors) >>> result 'red, blue, green, yellow' >>> result = '-'.join(colors) >>> result 'red-blue-green-yellow'
'개발 > Python' 카테고리의 다른 글
Python_ 파이썬 이차원 리스트, enumerate(), zip() (0) 2021.11.09 Python_ 파이썬 리스트 컴프리헨션(list comperhenshion), 필터링, 중첩 반복문 (0) 2021.11.09 Python_ 딕셔너리(dictionary) 함수 (0) 2021.11.08 Python_ 자료 구조, 튜플(tuple), 셋(set), 딕셔너리(dictionary) (0) 2021.11.08 Python_ 함수 개발 가이드라인 (0) 2021.11.08 댓글 (비로그인 댓글 허용하지 않습니다.)