Posts String Split and Join (Python 3)
Post
Cancel

String Split and Join (Python 3)

1
2
3
>>> a = "-".join(a)
>>> print a
this-is-a-string 
  • Task
    • You are given a string. Split the string on a “ “ (space) delimiter and join using a - hyphen.
  • Input Format
    • The first line contains a string consisting of space separated words.
  • Output Format
    • Print the formatted string as explained above.

문제풀이

  • line 이 주어지면 공백으로 split하여 리스트를 만들고, 그 리스트를 ‘-‘로 join하라는 문제
  • 정석으로 풀면 split -> join이 맞고 (1안)
  • 그냥 replace(변환함수)로 공백을 ‘-‘로 바꾸면 된다.(2안)
1
2
3
4
5
6
7
8
9
# 1안
def split_and_join(line):
    line = line.split()
    return '-'.join(line)
    
if __name__ == '__main__':
    line = input()
    result = split_and_join(line)
    print(result)
1
'this-is-a-string'
1
2
3
4
5
6
7
8
# 2안
def split_and_join(line):
    return line.replace(' ', '-')
    
if __name__ == '__main__':
    line = input()
    result = split_and_join(line)
    print(result)
This post is licensed under CC BY 4.0 by the author.