Mutations
def mutate_string(string, position, character):
s = string
l = list(string)
l[position] = character
return ''.join(l)
if __name__ == '__main__':
해설
>>> string = "abracadabra"
>>> l = list(string)
>>> l
['a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a']
>>> l[5] = 'k'
>>> l
['a', 'b', 'r', 'a', 'c', 'k', 'd', 'a', 'b', 'r', 'a']
''.join(l)
>>> 'abrackdabra'
지금까지 배운 문자열, 리스트, 인덱싱, 조인 등의 활용법이다
indexing
https://bohemihan.tistory.com/entry/HackerRank-Python-Solution-Lists
join
>>> words = ['this', 'is', 'a', 'string']
>>> "-".join(words)
'this-is-a-string'
join함수는 객체(iterable) 내의 문자사이에 '삽입할것'을 넣어주는 기능을 한다.
사용방법
삽입할것.join(iterable)
파라미터
· join 함수는 객체(iterable) 자리에 List, Tuple, Dictionary, Set 등을 받는다.
'Develop > hackerrank.com' 카테고리의 다른 글
HackerRank Python Solution / Find a string (0) | 2022.01.17 |
---|---|
HackerRank Python Solution / What's Your Name? (0) | 2022.01.17 |
HackerRank Python Solution / String Split and Join (0) | 2022.01.17 |
HackerRank Python Solution / sWAP cASE (0) | 2022.01.11 |
HackerRank Python Solution / Tuples (0) | 2022.01.11 |