Exercise 45
Write a python algorithm as a function which takes as parameters a string characters s and which returns another string obtained from s by replacing all the uppercase characters by the diese char '#'.
Solution
def replace_uppercase_with_hashtag(s): # Create an empty string to store the new string new_s = "" # Iterate over each character in the input string for char in s: # If the character is uppercase, replace it with "#" if char.isupper(): new_s += "#" # Otherwise, keep the original character else: new_s += char # Return the new string return new_s # Example input_str = "Hello World!" output_str = replace_uppercase_with_hashtag(input_str) print(output_str) # Output: "#ello #orld!"
In this example:
- the function takes the input string "Hello World!"
- and returns a new string "#ello #orld!" where all the uppercase characters have been replaced by the "#" character.
Younes Derfoufi
my-courses.net
[…] Exercise 45 || Solution […]