Function to Alter a Character in a String - Python Programming Exercise

In this exercise, you will develop a Python program with a function called "change_char" to alter a character at a specific position (0-based index) in a string, replacing it with another character. This exercise is perfect for practicing function definition, string manipulation, and indexing in Python. By implementing this function, you will gain hands-on experience in handling function definitions and string manipulation in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Function To Alter A Character In A String

 Objective

Develop a Python program with a function called "change_char" to alter a character at a specific position (0-based index) in a string, replacing it with another character.

For example:

sentence = "Tomato" change_char(sentence, 5, "a")

 Example Python Exercise

 Copy Python Code
# Define the function to change a character at a specific position
def change_char(sentence, position, new_char):
    # Convert the string to a list of characters (since strings are immutable)
    sentence_list = list(sentence)
    
    # Replace the character at the specified position with the new character
    sentence_list[position] = new_char
    
    # Convert the list back to a string and return it
    return ''.join(sentence_list)

# Main function to test the change_char function
def main():
    sentence = "Tomato"
    # Call change_char to replace the character at index 5 with 'a'
    result = change_char(sentence, 5, "a")
    print(result)  # Expected output: "Tomata"

# Call the main function to execute the program
main()

 Output

Tomata

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Function to Determine Prime Status

    In this exercise, you will develop a Python program with a function called "is_prime" that takes an integer as input and returns True if the number is prime, or False...

  •  Main Function Parameters and Summation

    In this exercise, you will develop a Python program named "sum" that takes two integer numbers from the command line and prints their sum. This exercise is per...

  •  Function to Calculate Sum of Digits

    In this exercise, you will develop a Python program with a function named "sum_digits" that takes a number as input and returns the sum of its digits. This exercis...

  •  Function to Compute Factorial

    In this exercise, you will develop a Python program with a recursive function to compute the factorial of a given number. The factorial of a number is defined as: 𝑛...

  •  Main Function Parameters and Reversal

    In this exercise, you will develop a Python program named "reverse" that takes multiple words from the command line and prints them in reverse order. This exercise...

  •  Function to Obtain Integer Value

    In this exercise, you will develop a Python program with a function named "get_int" that displays the text received as a parameter, prompts the user for an integer, a...