Hanwool Codes RSS Tag Admin Write Guestbook
2023-02-14 06:03:58

Mutations | HackerRank

 

Mutations | HackerRank

Understand immutable vs mutable by making changes to a given string.

www.hackerrank.com

 

Goal

 

Read a given string, change the character at a given index and then print the modified string.

 

Solution

 

We need to understand slicing and indexing Strings in Python. You can check this link

 

 

#Mutations

def mutate_string(string, position, character):
    
    new_string = string[:position] + character + string[position+1:]
    
    return new_string

if __name__ == '__main__':
    s = input()
    i, c = input().split()
    s_new = mutate_string(s, int(i), c)
    print(s_new)

 

Hanwool Codes. Designed by 코딩재개발.