파이썬 join, split 차이점
str.join() 함수란?
Python의 str.join()
함수는 리스트나 튜플과 같은 반복 가능한(iterable) 객체의 모든 아이템을 연결하여 하나의 문자열을 만듭니다. 이 함수는 문자열 조작 작업에서 매우 유용합니다.
str.split() 함수란?
반면에 str.split()
함수는 문자열을 지정된 구분자를 기준으로 분리하여 리스트를 반환합니다.
두 함수의 주요 차이점
둘의 주요 차이점은 str.join()
이 여러 개의 문자열을 하나로 합치는 반면, str.split()
는 하나의 문자열을 여러 개로 분리한다는 것입니다.
str.join() 예시 1
Bash
words = ['Hello', 'world']
result = ' '.join(words)
print(result)
Output: Hello world
str.split() 예시 1
Python
text = 'Hello world'
result = text.split(' ')
print(result)
Output: ['Hello', 'world']
str.join() 예시 2
Bash
letters = ['a', 'b', 'c']
result = ','.join(letters)
print(result)
Output: a,b,c
str.split() 예시 2
Python
text = 'a,b,c'
result = text.split(',')
print(result)
Output: ['a', 'b', 'c']
str.join() 예시 3
Bash
nums = ['1', '2', '3']
result = '+'.join(nums)
print(result)
Output: 1+2+3
str.split() 예시 3
Python
text = '1+2+3'
result = text.split('+')
print(result)
Output: ['1', '2', '3']
str.join() 예시 4
Bash
sentence = ['Python', 'is', 'easy']
result = ' '.join(sentence)
print(result)
Output: Python is easy
str.split() 예시 4
Python
text = 'Python is easy'
result = text.split(' ')
print(result)
Output: ['Python', 'is', 'easy']
더 알아보기
더 자세한 내용은 Python 공식 문서의 str.join()과 str.split() 섹션에서 확인할 수 있습니다.