Problem of the Day

We have seen that lists are mutable (they can be changed), and tuples are immutable (they cannot be changed).

Let's try to understand this with an example.

You are given an immutable string, and you want to make changes to it.

Example

>>> string = "abracadabra"
        

You can access an index by:

>>> print string[5]
a
        

What if you would like to assign a value?

>>> string[5] = 'k' 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
        

How would you approach this?

  • One solution is to convert the string to a list and then change the value.

Example

>>> string = "abracadabra"
>>> l = list(string)
>>> l[5] = 'k'
>>> string = ''.join(l)
>>> print string
abrackdabra
        

  • Another approach is to slice the string and join it back.

Example

>>> string = string[:5] + "k" + string[6:]
>>> print string
abrackdabra
        

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

Complete the mutate_string function in the editor below.

mutate_string has the following parameters:

  • string string: the string to change
  • int position: the index to insert the character at
  • string character: the character to insert

Returns

  • string: the altered string

Input Format

The first line contains a string, . The next line contains an integer , the index location and a string , separated by a space.

Sample Input

STDIN           Function
-----           --------
abracadabra     s = 'abracadabra'
5 k             position = 5, character = 'k'
        

Sample Output

abrackdabra        

要查看或添加评论,请登录

KUNAL V.的更多文章

  • Solution of the Day

    Solution of the Day

    def print_full_name(first, last): # Write your code here string = "Hello"+" "+first+" "+last+"! You just delved into…

  • Solution of the Day

    Solution of the Day

    def split_and_join(line): # write your code here line = line.split(" ") line = "-".

  • Problem of The Day

    Problem of The Day

    You are given a string and your task is to swap cases. In other words, convert all lowercase letters to uppercase…

  • Problem of the Day

    Problem of the Day

    Print the following pattern Write a program to print the following start pattern using the loop Solution for the same…

  • Solution of The Day

    Solution of The Day

    if name == '__main__': n = int(input()) integer_list = tuple(map(int, input().split())) has_value = hash(integer_list)…

  • Solve this Problem

    Solve this Problem

    Given an integer, , and space-separated integers as input, create a tuple, , of those integers. Then compute and print…

  • Answer For Today's Coding Problem

    Answer For Today's Coding Problem

    if name == '__main__': # Read the number of operations N = int(input()) # Initialize an empty list L = [] # Loop…

  • How would you approach this problem? Comment your answer

    How would you approach this problem? Comment your answer

    Consider a list (list = []). You can perform the following commands: insert i e: Insert integer at position .

  • Python Problem Solving

    Python Problem Solving

    2/ 30 Day Solution #python #pythonproblemsolving #30daycodingchallenge #30daychallenge #problemsolving if name ==…

社区洞察

其他会员也浏览了