[Python] Split & Join

미남로그·2021년 12월 24일
0

그전에도 지금도 앞으로도 파이썬 공부는 쭉 계속된다.🚀

파이썬 기반 다지기 공사 들어갑니다.... 영차영차 🧱🧱🧱🧱🧱



목표

  1. Split & Join 을 사용하여 String Type 의 값을 List 형태로 변환하기
  2. List Type의 값을 String Type의 값으로 변환하는 코드 작성법


핵심 키워드

  • split
  • join
  • string
  • list
  • unpacking


Split Type의 값을 나눠서 List 형태로 변환

빈칸을 기준으로 문자열 나누기

items = 'zero one two three'.split()
print(items)

Out:
['zero', 'one', 'two', 'three']


"," 을 기준으로 문자열 나누기

example = 'python, query, javascript'
print(example.split(','))

Out:
['python,', 'query,', 'javascript']


리스트의 각 값을 a, b, c, 변수로 unpacking

a, b, c = example.split(',')
print(a, b, c)

Out:
python query javascript


"."을 기준으로 문자열 나누고 unpacking

example = 'python.c++.java'
one, two, three = example.split('.')
print(one, two, three)

Out:
python c++ java




Join 함수

-join: String List를 합쳐 하나의 String으로 반환할 때 사용

colors = ['red', 'blue', 'green', 'yellow']
result = ''.join(colors)
print(result)

Out:
redbluegreenyellow


# 연결 시 빈칸 1칸으로 연결
result = ' '.join(colors)
print(result)

Out:
red blue green yellow


# 연결 시 ", "으로 연결
result = ','.join(colors)
print(result)

Out:
red, blue, green, yellow


result = '-'.join(colors)
result

Out:
red-blue-green-yellow


여기까지 String Type을 List type으로 나누는 함수 split, List type을 String Type으로 합치는 함수 join을 살펴보았습니다!


Reference

🔗 머신러닝을 위한 파이썬 강의

무료 강의입니다! 강추!

profile
미남이 귀엽죠

0개의 댓글