[Python] 파이썬 join()과 split() 함수 차이점

2023. 9. 18. 08:21·Python

파이썬 join, split 차이점

str.join() 함수란?

Python의 str.join() 함수는 리스트나 튜플과 같은 반복 가능한(iterable) 객체의 모든 아이템을 연결하여 하나의 문자열을 만듭니다. 이 함수는 문자열 조작 작업에서 매우 유용합니다.

str.split() 함수란?

반면에 str.split() 함수는 문자열을 지정된 구분자를 기준으로 분리하여 리스트를 반환합니다.

두 함수의 주요 차이점

둘의 주요 차이점은 str.join()이 여러 개의 문자열을 하나로 합치는 반면, str.split()는 하나의 문자열을 여러 개로 분리한다는 것입니다.

str.join() 예시 1


words = ['Hello', 'world']
result = ' '.join(words)
print(result)

Output: Hello world

str.split() 예시 1


text = 'Hello world'
result = text.split(' ')
print(result)

Output: ['Hello', 'world']

str.join() 예시 2


letters = ['a', 'b', 'c']
result = ','.join(letters)
print(result)

Output: a,b,c

str.split() 예시 2


text = 'a,b,c'
result = text.split(',')
print(result)

Output: ['a', 'b', 'c']

str.join() 예시 3


nums = ['1', '2', '3']
result = '+'.join(nums)
print(result)

Output: 1+2+3

str.split() 예시 3


text = '1+2+3'
result = text.split('+')
print(result)

Output: ['1', '2', '3']

str.join() 예시 4


sentence = ['Python', 'is', 'easy']
result = ' '.join(sentence)
print(result)

Output: Python is easy

str.split() 예시 4


text = 'Python is easy'
result = text.split(' ')
print(result)

Output: ['Python', 'is', 'easy']

더 알아보기

더 자세한 내용은 Python 공식 문서의 str.join()과 str.split() 섹션에서 확인할 수 있습니다.

저작자표시 비영리 변경금지 (새창열림)
'Python' 카테고리의 다른 글
  • [Python] set과 drop_duplicates 중복 값 처리의 차이점
  • [Python] 파이썬 enumerate, zip 차이
  • [Python] 파이썬 removeprefix, removesuffix 란?
  • [Python] 파이썬 sys 모듈이란?
hyunicecream
hyunicecream
안녕하세요. 여러가지 정보를 통해 조금이나마 도움이 되고자 시작하게 되었습니다.
  • hyunicecream
    Café
    hyunicecream
  • 홈
  • 포스팅 카테고리

    • 분류 전체보기
      • 생성형 AI
        • ChatGPT
        • Claude AI
        • MS Copilot
        • Perplexity
        • 생성형 AI 정보
        • 프롬프트 작성 가이드
        • AI 활용기
      • Notion
      • 업무 생산성
      • Python
        • pandas
      • 머신러닝
      • 딥러닝
      • Terminal
      • SQL
        • Postgre SQL
      • 여행정보
        • 국내여행
  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
hyunicecream
[Python] 파이썬 join()과 split() 함수 차이점
상단으로

티스토리툴바