Prepare > Python > Strings > Mutations
2023. 7. 2. 19:22ㆍHackerRank-Python
Mutations | HackerRank
Understand immutable vs mutable by making changes to a given string.
www.hackerrank.com
문제
Read a given string, change the character at a given index and then print the modified string.
# INPUT
abracadabra
5 k
=> "abracadabra"의 5번 인덱스를 'k'로 바꾸어 출력하라.
코드
def mutate_string(string, position, character):
return string[:position] + character + string[position+1:]
if __name__ == '__main__':
노트
문자열도 대괄호([ ])로 인덱싱 가능
ANOTHER CODE
def mutate_string(string, position, character):
li = list(string)
li[position] = character
return "".join(li)
if __name__ == '__main__':
- 리스트 -> 인덱스값 변경 -> 문자열
'HackerRank-Python' 카테고리의 다른 글
Prepare > Python > Basic Data Types > Tuples (0) | 2023.07.05 |
---|---|
Prepare > Python > Strings > Find a string (0) | 2023.07.04 |
Prepare > Python > Strings > What's Your Name? (0) | 2023.06.30 |
Prepare > Python > Strings > String Split and Join (0) | 2023.06.29 |
Prepare > Python > Strings > sWAP cASE (0) | 2023.06.28 |