Exercise

Develop a Python program that replaces characters at odd indices in a given string with '#'. For instance, if the input string is 'Python', the algorithm should produce the resulting string: 'P#t#o#'.

Solution

def replace_odd_characters(s):
    result = ''
    for i in range(len(s)):
        if i % 2 == 1:  # Check if the index is odd
            result += '#'
        else:
            result += s[i]
    return result

# Example usage:
input_string = 'Python'
output_string = replace_odd_characters(input_string)
print(output_string)
# output : P#t#o#




 

Younes Derfoufi
CRMEF OUJDA

One thought on “Solution Exercise 74 : Replace Characters at Odd Indices”

Leave a Reply