[ 난이도: Easy | 분야: Strings ]
파이썬에는, 구분기호로 문자열을 분리할 수 있다.
a = "this is a string"
a = a.split(" ") # a is converted to a list of strings
print a
결과: ['this','is','a','string']
반대로 문자열을 연결할 수도 있다.
a = "-".join(a)
print a
결과: this-is-a-string
주어진 문자열을 " "(공백)으로 분리시킨 뒤, - 하이픈으로 연결하라.
아래의 에디터에 split_and_join 함수를 완성하라.
split_and_join은 다음 파라미터들을 따른다:
- string line: 단어들이 공백으로 구분되어 있는 문자열
- string: 결과 문자열
공백으로 구분된 단어들로 이루어진 한 줄의 문자열
this is a string
this-is-a-string
def split_and_join(line):
# write your code here
if __name__ == '__main__':
line = input()
result = split_and_join(line)
print(result)
더보기
정답
def split_and_join(line):
# write your code here
line = line.split(" ")
line = "-".join(line)
return line
if __name__ == '__main__':
line = input()
result = split_and_join(line)
print(result)
©️Hackerrank. All Rights Reserved.