티스토리 뷰

.split()
Signature:
.split(self, sep=None, maxsplit=-1)
Docstring: Return a list of the words in the string, using sep as the delimiter string.
Type:
method_descriptor​
.split()
  • str 내장 함수
  • 괄호안의 첫번째 파라메터 문자를 구분자로 하여 문자열을 나눠 준다.
    구분자(delimiter) : 임의의 기호로 성립되는 열을 구성 요소로 구분 짓기 위한 문자
  • . 앞에 str(문자열)을 입력
  • 기존 str(문자열)을 나누어 list로 반환한다.
    →                list type으로 반환

  • sep = 
                    나눌 기준이될 문자열을 입력받는다.
    →                파라메터에 입력값이 없다면 모든 빈공백을 기준으로 나눠준다.
                    = 공백, 탭{ \t }, 엔터 { \n } 전부 구분자로 써 나눠준다.

  • maxsplit =
                    최대로 나눌 수
                   
    default: -1(제한없이 최대로 나누라는 의미)

사용예제:
test_split = 'Hello world and F7project'.split('o')  # 'o'를 구분자로 사용
print(test_split)
['Hell', ' w', 'rld and F7pr', 'ject']

 

str.split(' '), str.split() 차이점
test_str = '''Hello
world    and F7project'''
print(test_str)
Hello
world    and F7project

 

- str.split(' ')

test_split = test_str.split(' ')
print(test_split)
['Hello\nworld', '', '', '', 'and', 'F7project']

 

str.split()

test_split = test_str.split()
print(test_split)
['Hello', 'world', 'and', 'F7project']

 

maxsplit=

 

- maxsplit=1

test_split = 'Hello world and F7project'.split(maxsplit=1)
print(test_split)
['Hello', 'world and F7project']

 

- maxsplit=2

test_split = 'Hello world and F7project'.split(maxsplit=2)
print(test_split)
['Hello', 'world', 'and F7project']

 

 


.rsplit()
Signature:
.rsplit(self, sep=None, maxsplit=-1)
Docstring:
Return a list of the words in the string, using sep as the delimiter string.
Type:
method_descriptor​
.rsplit()
  • .split() 함수와 같은 기능
  • 기존에는 왼쪽에서 오른쪽으로 적용되었지만 rsplit()은 reversed하여 오른쪽에서 왼쪽으로 적용 된다.

사용예제:

- 기존 str.split(maxsplit=1)

test_split = 'Hello world and F7project'.split(maxsplit=1)
print(test_split)
['Hello', 'world and F7project']

 

- str.rsplit(maxsplit=1)

 

test_split = 'Hello world and F7project'.rsplit(maxsplit=1)
print(test_split)
['Hello world and', 'F7project']
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
글 보관함