Menu
Coddy logo textTech

문자열 순회하기 파트 2

Coddy Python 여정의 Fundamentals 섹션에 포함된 레슨 — 77개 중 63번째.

문자열 분할을 사용하면 문자열을 리스트로 나눌 수 있으며, 결합을 사용하면 리스트 항목들을 하나의 문자열로 합칠 수 있습니다.

split() 메서드는 구분자를 기준으로 문자열을 하위 문자열의 리스트로 나눕니다.

공백으로 분할:

text = "apple banana cherry"
fruits = text.split()
print(fruits)
# ['apple', 'banana', 'cherry']

특정 구분자로 분할하기:

data = "john,25,new york"
data = data.split(',')
print(data)
# ['john', '25', 'new york']

join() 메서드는 이터러블의 요소들을 하나의 문자열로 결합합니다.

기본적인 결합:

words = ['Hello', 'World', 'Python']
text = ' '.join(words)
print(text)
# "Hello World Python"

다른 구분자로 연결하기:

fruits = ['apple', 'banana', 'cherry']
line = ','.join(fruits)
print(line)
# "apple,banana,cherry"
challenge icon

챌린지

쉬움

텍스트 문자열과 구분자 문자라는 두 개의 입력을 받는 프로그램을 작성하세요. 프로그램은 텍스트를 공백을 기준으로 단어로 나눈 다음, 지정된 구분자 문자를 사용하여 이 단어들을 결합하고 결과 문자열을 출력해야 합니다.

치트 시트

Python에서의 문자열 분할 및 결합:

문자열을 리스트로 분할하기:

text = "apple banana cherry"
fruits = text.split()  # 공백으로 분할
print(fruits)  # ['apple', 'banana', 'cherry']

data = "john,25,new york"
info = data.split(',')  # 쉼표로 분할
print(info)  # ['john', '25', 'new york']

리스트를 문자열로 결합하기:

words = ['Hello', 'World', 'Python']
text = ' '.join(words)  # 공백으로 결합
print(text)  # "Hello World Python"

fruits = ['apple', 'banana', 'cherry']
line = ','.join(fruits)  # 쉼표로 결합
print(line)  # "apple,banana,cherry"

직접 해보기

text = input()
delimiter = input()
# 아래에 코드를 작성하세요
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

Fundamentals의 모든 레슨