• 회원가입
  • 로그인
  • 구글아이디로 로그인

[string] Python - split() 메서드 ★ - 왼쪽부터 문자열을 지정 구분자 기준으로 쪼개기 후 List 생성. (= split메서드 = 스플릿 메서드)

2678  

목차

  1. split() 예제 - 공백 기준으로 잘라서 List 생성
  2. split() 정의
  3. split() 구문
  4. split() 예제 - 공백 외 기준으로 잘라서 List 생성
  5. split() 예제 - maxsplit 매개변수 설정
  6. split() 예제 - 쪼개기 후, 각각의 변수에 담기 ★

 

split() 예제 - 공백 기준으로 잘라서 List 생성

 

txt = "홈짱닷컴 Homzzang.com"

x = txt.split()

print(x)


결과값: ['홈짱닷컴', 'Homzzang.com']

 

split() 정의

 

왼쪽부터, 문자열을 지정 구분자 (기본값: 공백) 기준으로 잘라서 List 생성.

maxsplit 매개변수 설정 시, List 요소 개수는 maxsplit + 1 개.

input() 함수와 함께 사용 시, 한번에 여러 숫자 입력받기 가능. 

 


cf.

rsplit() 메서드 : 오른쪽부터 쪼개기 후 List 생성.

join() 메서드 : 반복 가능 객체의 요소를 지정 구분자 기준으로 합치기

 

 

split() 구문

 

string.split(separator, maxsplit)

 


[매개변수]

 

separator

선택. 구분자. (기본값: 공백)

 

maxsplit

선택. 쪼갤 지점 최대 개수.

※ 반환되는 List 요소 개수 : maxsplit + 1

maxsplit 설정 시, seperator 매개변수 지정 필수.

 


[반환값]

 

작은따옴표(')로 묶인 문자열 형태로 쪼개진 문자열 반환.

 

 

split() 예제 - 공백 외 기준으로 잘라서 List 생성

[예제1] - 쉼표와 공백을 구분자로 사용.

 

txt = "홈짱닷컴, Homzzang.com, 코딩강의"

x = txt.split(", ")

print(x)

 

결과값: ['홈짱닷컴', 'Homzzang.com', '코딩강의']


[예제2] - # 기호를 구분자로 사용.

 

txt = "홈짱닷컴#Homzzang.com#코딩강의"

x = txt.split("#")

print(x)

 

결과값: ['홈짱닷컴', 'Homzzang.com', '코딩강의'] 

 

split() 예제 - maxsplit 매개변수 설정

※ maxsplit 설정 시 요소 개수는 maxsplit + 1 개

 

txt = "홈짱닷컴 Homzzang.com 코딩강의"

x = txt.split(" ", 1)

print(x)


결과값: ['홈짱닷컴', 'Homzzang.com 코딩강의']

 

split() 예제 - 쪼개기 후, 각각의 변수에 담기 ★

 

hz = "홈짱닷컴 Homzzang.com"

site,host = hz.split()

print (site) # 홈짱닷컴

print(host) # Homzzang.com 

 



분류 제목
numpy PY - NumPy ufuncs : Simple Arithmetic (간단 산수)
numpy PY - NumPy ufuncs : Rounding Decimals (반올림 소수점)
numpy PY - NumPy ufuncs : Logs (로그)
numpy PY - NumPy ufuncs : Summations (합계=합산)
numpy PY - NumPy ufuncs : Products (요소 곱셈)
numpy PY - NumPy ufuncs : Differences (요소의 차)
numpy PY - NumPy ufuncs : Finding LCM (최소공배수 찾기)
numpy PY - NumPy ufuncs : Finding GCD (최대공약수 찾기)
numpy PY - NumPy ufuncs : Trigonometric Functions (삼각함수)
numpy PY - NumPy ufuncs : Hyperbolic Functions (쌍곡선함수)
numpy PY - NumPy ufuncs : Set Operations (집합 연산)
howto Python - 3과 5의 배수의 합산(합계)
basic Python - Math (수학)
module Python - statistics 모듈 메서드 종류 (= 통계 모듈)
module Python - statistics.harmonic_mean() 메서드 - 조화 평균값 (= harmonic…
module Python - statistics.mean() 메서드 ★ - 평균값 (= mean메서드 = 민메서드)
module Python - statistics.median() 메서드 ★ - 중앙값 (= 중간값 = 가운데값 = med…
module Python - statistics.median_grouped() 메서드 -
module Python - statistics.median_high() 메서드 - 높은 중앙값 (= median_hig…
module Python - statistics.median_low() 메서드 - 낮은 중앙값 (= median_low메…
18/24
목록
찾아주셔서 감사합니다. Since 2012