티스토리 뷰
728x90
String Split and Join
In Python, a string can be split on a delimiter.
Example:
>>> a = "this is a string"
>>> a = a.split(" ") # a is converted to a list of strings.
>>> print a
['this', 'is', 'a', 'string']
Joining a string is simple:
>>> 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.
Function Description
Complete the split_and_join function in the editor below.
split_and_join has the following parameters:
- string line: a string of space-separated words
Returns
- string: the resulting string
Input Format
The one line contains a string consisting of space separated words.
Sample Input
this is a string
Sample Output
this-is-a-string
문제해석
입력받은 문자열에서 " " 공백을 - 하이픈으로 변경하여 출력한다.
문제풀이
개인적으로 Basic Data Types 코스는 너무 어려웠는데... Strings가 먼저 나오는게 맞지 않나 싶다...;
문제에서 split은 문자열을 분리하는 함수이고 join은 다시 합칠 수 있는 함수라고 설명해 주었으므로,
split(" ")함수를 사용하여 문자열을 분리한 후 '-'.join()하면 - 하이픈으로 구분하여 다시 합칠 수 있다.
def split_and_join(line):
# write your code here
return '-'.join(line.split(" "))
if __name__ == '__main__':
line = raw_input()
result = split_and_join(line)
print result
728x90
LIST
'Programming > Python' 카테고리의 다른 글
[Python/Hackerrank] Strings > Mutations (0) | 2021.12.08 |
---|---|
[Python/Hackerrank] Strings > What's Your Name? (0) | 2021.12.08 |
[Python/Hackerrank] Strings > sWAP cASE (0) | 2021.12.08 |
[Python/Hackerrank] Basic Data Types > Tuples (0) | 2021.12.08 |
[Python/Hackerrank] Basic Data Types > Lists (0) | 2021.12.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- MS SQL Server
- Tableau
- 몰누피라비르
- 리비안
- MSSQL
- 넷플릭스
- 매매일지
- SQL Server
- tensorflow
- TSQL
- hackerrank
- HK이노엔
- Weather Observation Station
- 테슬라
- 에코캡
- python
- 코로나19
- insert
- string
- DATABASE
- list
- 경구치료제
- mysql
- 해커랭크
- 동국알앤에스
- 분석탭
- python3
- 미중무역전쟁
- 대원화성
- 넥스트BT
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함