Mutations

  • + 0 comments
    def mutate_string(string: str, position: int, character: str) -> str:
        """Replace the character at the given position with a new one."""
        
        if not (0 < position <= len(string)):
            raise ValueError("Position out of range")
    
        return string[:position] + character + string[position + 1:]
    
    if __name__ == '__main__':
        s = input()
        i, c = input().split()
        s_new = mutate_string(s, int(i), c)
        print(s_new)