Python에서 split()
을 사용하여 특정 문자를 기준으로 문자열을 분리할 수 있습니다.
Syntax는 다음과 같습니다.
string.split(separator, maxsplit)
- separator: 문자열을 나눌 때 사용할 문자입니다. 기본 값은 whitespace입니다.
- maxsplit: 문자열을 나눌 횟수를 지정합니다. 기본 값은 -1이며, 제한 없이 모두 나눈다는 의미입니다.
split() 예제
다음과 같은 문자열이 주어졌을 때, split()
은 whitespace를 기준으로 문자열을 나눕니다.
text = 'Hello world, python'
strings = text.split()
print(strings)
Output:
['Hello', 'world,', 'python']
split(',')
은 ,
를 기준으로 문자열을 나눕니다.
text = 'Hello world, python'
strings = text.split(',')
print(strings)
Output:
['Hello world', ' python']
다음과 같이 :
로 나눌 수도 있습니다.
text = 'Hello:world:python'
strings = text.split(':')
print(strings)
Output:
['Hello', 'world', 'python']
split(separator, maxsplit) 예제
maxsplit을 설정하면 최대 나눌 횟수를 지정할 수 있습니다.
다음과 같이, split(',', 0)는 ,
로 문자열을 나누지만 최대 0번까지만 나누라는 의미입니다.
text = 'Hello, world, python'
strings = text.split(',', 0)
print(strings)
결과를 보면, 문자열을 나누지 않았고 문자열 전체가 리스트에 추가되었습니다.
['Hello, world, python']
split(',', 1)
는 최대 1회까지만 문자열을 나눕니다.
text = 'Hello, world, python'
strings = text.split(',', 1)
print(strings)
Output:
['Hello', ' world, python']
split(',', 2)
는 최대 2회까지 문자열을 나눕니다.
text = 'Hello, world, python'
strings = text.split(',', 2)
print(strings)
Output:
['Hello', ' world', ' python']
split(',', -1)
는 횟수 제한 없이 나눌 수 있는 만큼 문자열을 나눕니다.
text = 'Hello, world, python'
strings = text.split(',', -1)
print(strings)
Output:
['Hello', ' world', ' python']
Recommended Posts:
- Python - dict 정렬 (Key, Value로 sorting)
- Python - String을 bytes로 변경하는 방법
- Python - bytes를 String으로 변환하는 방법
- Python - 리스트에서 첫번째, 마지막 요소 가져오는 방법
- Python - 문자열에서 특정 단어 추출
- Python - float을 int로 변경하는 방법
- Python - 특정 문자열로 시작하는 문자열 찾기
- Python - dictionary의 중복 제거 방법
- Python - 리스트가 비어있는지 확인
- Python - 문자열 뒤집기, 문자열 거꾸로 출력
- Python - 현재 날짜, 시간 가져오는 방법
- Python - 두개의 리스트 하나로 합치기
- Python - 리스트 크기(size) 구하기