이 자료는 네이버 부스트 코스에서 제공하는 자료로 만들어졌습니다.
-split 함수
string type의 값을 “기준값”으로 나눠서 List 형태로 변환
>>> items = 'zero one two three'.split() # 빈칸을 기준으로 문자열 나누기
>>> print (items)
['zero', 'one', 'two', 'three']
>>> example = 'python,java,javascript' # ","을 기준으로 문자열 나누기
>>> example.split(",")
['python', ‘java', 'javascript']
>>> a, b, c = example.split(",")
# 리스트에 있는 각 값을 a,b,c 변수로 unpacking
>>> example = ‘teamlab.technology.io'
>>> subdomain, domain, tld = example.split('.')
# "."을 기준으로 문자열 나누기 → Unpacking
split()함수는 문자열을 리스트로 분리해준다.
-join 함수
- String으로 구성된 list를 합쳐 하나의 string으로 반환
>>> colors = ['red', 'blue', 'green', 'yellow']
>>> result = ''.join(colors)
>>> result
'redbluegreenyellow'
>>> result = ' '.join(colors) # 연결 시 빈칸 1칸으로 연결
>>> result
'red blue green yellow'
>>> result = ', '.join(colors) # 연결 시 ", "으로 연결
>>> result
'red, blue, green, yellow'
>>> result = '-'.join(colors) # 연결 시 "-"으로 연결
>>> result
'red-blue-green-yellow'
반응형
'AI > python AI' 카테고리의 다른 글
python - function passing arguments (0) | 2022.05.30 |
---|---|
python - iterable object & generator (0) | 2022.05.30 |
python - lambda & map & reduce (0) | 2022.05.30 |
python - enumerate,zip (0) | 2022.05.30 |
python - list (0) | 2022.05.30 |