티스토리 뷰
.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']'Python > Analysis to Python' 카테고리의 다른 글
| [sum] : 모든 items를 더하는 함수 (0) | 2022.08.03 |
|---|---|
| [f-string] : 문자열 포맷팅 딥하게 알아보기 + Tip (0) | 2022.07.27 |
| [append an element in list] : 속도 비교(performance testing ) (0) | 2022.06.07 |
| [eval, exec] : 문자열을 코드로 실행하는 함수 (0) | 2022.05.30 |
| [sort, sorted] : 정렬 함수 (0) | 2022.05.29 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Python
- _의미
- sdsad
- 변수 덮어쓰기
- 덮어쓰기
- asd ad
- 파이썬 변수
- 재귀?
- underscore
- 콘다
- 이스케이프 코드
- list comprehension
- sad asd
- conda
- 재귀함수 이해
- 백준
- parameters
- 이중 프린트
- print()
- recursive
- anaconda
- _meaning
- recursive function
- matplotlib
- sep=
- d asd asd
- arguments
- 재귀함수 설명
- 파이썬
- 연산속도
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함