Exercise 74
Write a Python program that replace the characters with odd index of a given string by '#'. Exemple if s = "Python" , the algorithm returns the string: "P#t#o#"
Solution
def replace(s):
n = len(s)
# initializing desired string
new_string = ""
# iterate over all characters of string s
for i in range(0 , n):
# adding the character of even index to the new string
if( i%2 == 0 ):
new_string = new_string + s[i]
else:
new_string = new_string + '#'
return new_string
# Example
s = "Python"
print(replace(s)) # display : P#t#o#
Younes Derfoufi
my-courses.net
my-courses.net