본문 바로가기
Study/Python study

[Python(파이썬)] split 함수

by 파크영 2021. 12. 1.

split()

문자열을 일정한 규칙으로 잘라서 리스트로 만들어주는 함수

 

split의 모양

문자열.split()
문자열.split('구분자')
문자열.split('구분자', 분할횟수)
문자열.split('구분자', maxsplit=분할횟수)

 

sep 파라미터 : 기본값 none (띄어쓰기, 엔터를 구분자로 하여 문자열을 나눈다.)
maxsplit 파라미터 : 기본값 -1 (제한없이 자를 수 있을 때까지 문자열 전체를 나눈다.)

 

 

  • 문자열.split()

split()에 아무것도 입력하지 않으면 기본적으로 띄어쓰기, 엔터를 구분하여 문자열을 나누게 된다. 

또한 maxsplit 파라미터도 정하지 않았기 때문에 나눌 수 있는 최대로 나누게 된다.  

>>> test = '가 나 다 라 마 바'
>>> print(test.split())
['가', '나', '다', '라', '마', '바']

>>> test2 = '1 2 3 4 5 6'
>>> print(test2.split())
['1', '2', '3', '4', '5', '6']

>>> test3 = 'a b c d e'
>>> print(test3.split())
['a', 'b', 'c', 'd', 'e']

 

 

  • 문자열.split('구분자') or 문자열.split(sep='구분자')

문자열.split(구분자)는 정해준 구분자를 기준으로 구분하여 문자열을 나눈다.

>>> test = '가,나,다,라,마,바'
>>> print(test.split(','))
['가', '나', '다', '라', '마', '바']

>>> test2 = '1-2-3-4-5'
>>> print(test2.split('-'))
['1', '2', '3', '4', '5']

>>> test3 = 'a+b+c+d+e'
>>> print(test3.split('+'))
['a', 'b', 'c', 'd', 'e']

>>> test = '가.나.다'
>>> print(test.split(sep='.'))
['가', '나', '다']


>>> test = '가, 나, 다, 라, 마, 바'	# ,뒤에 띄어쓰기를 했다면
>>> print(test.split(','))
['가', ' 나', ' 다', ' 라', ' 마', ' 바']	# 띄어쓰기도 함께 저장됨

 

 

  • 문자열.split('구분자', 분할횟수) or 문자열.split('구분자', maxsplit=분할횟수)
>>> test = '가,나,다,라,마,바'

>>> print(test)
가,나,다,라,마,바

>>> print(test.split())
['가,나,다,라,마,바']

>>> print(test.split(','))
['가', '나', '다', '라', '마', '바']

>>> print(test.split(sep=','))
['가', '나', '다', '라', '마', '바']

>>> print(test.split(',', 4))
['가', '나', '다', '라', '마,바']
# 4번만 분할한 후 마지막 '마,바'는 함께 저장됨

 

>>> a = 'a b c d e'

>>> print(a)
a b c d e

>>> print(a.split())
['a', 'b', 'c', 'd', 'e']

# split(1)은 불가함
>>> b = a.split(1)
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    b = a.split(1)
TypeError: must be str or None, not int

# 구분자 작성 후 사용 가능
>>> b = a.split(' ', 1)
>>> b
['a', 'b c d e']

 

문자열.split(2) - TypeError

TypeError: must be str or None, not int -> split 괄호 안에 파라미터를 한개만 사용할 때는 구분자를 뜻하는 것('maxsplit='을 작성하지 않았을 경우) 이기 때문에 none이나 문자열을 사용해야함, int는 불가하다. 

 

문자열.split('구분자', 2)와 같이 구분자를 작성한 후 maxsplit을 사용해야 Error 발생 안한다. 

단, 문자열.split(maxsplit=2)와 같이 'maxsplit='을 작성한다면 가능하다. 

 

>>> test = 'a b c d e f'

# 문자열 test 출력
>>> print(test)
a b c d e f

# sep 기본값(띄어쓰기 기준)으로 split
>>> print(test.split())
['a', 'b', 'c', 'd', 'e', 'f']

# split(1) ERROR - 구분자 없이 maxsplit 작성 불가('maxsplit =' 작성 안했을 시)
>>> print(test.split(1))
Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
    print(test.split(1))
TypeError: must be str or None, not int

# 구분자에 아무것도 없으면 ERROR 
>>> print(test.split('',1))
Traceback (most recent call last):
  File "<pyshell#55>", line 1, in <module>
    print(test.split('',1))
ValueError: empty separator

# 구분자 ' ', 1('maxsplit =' 작성 안함) 
>>> print(test.split(' ',1))
['a', 'b c d e f']

# 구분자 ' ', 1('maxsplit =' 작성함) 
>>> print(test.split(' ',maxsplit = 2))
['a', 'b', 'c d e f']

# 구분자 없이 maxsplit만 작성('maxsplit ='을 작성해야함) 
>>> print(test.split(maxsplit = 3))
['a', 'b', 'c', 'd e f']

 

댓글