Write a Python program to remove the characters which have odd index values of a given string


This program is written in the Python programming language and it removes all characters from an input string that are at odd-numbered indices. It does this using a for loop and an if statement:

  • The for loop iterates over the indices of the characters in the input string "Computer Education".
  • For each iteration of the loop, the program checks if the current index is even using the expression "i % 2 == 0". If this expression evaluates to True, then the character at that index is added to a new string "res".
  • If the expression "i % 2 == 0" evaluates to False, then the character at that index is skipped and not added to the "res" string.

Finally, the program prints the original string "Computer Education" and the modified string "res" (which only includes characters from even-numbered indices) to the console.

Source Code

str="Computer Education"
res = ""
print("Original String :",str) 
for i in range(len(str)):
	if i % 2 == 0:
	  res = res + str[i]
print("Remove Odd Index Char :",res)

Output

Original String : Computer Education
Remove Odd Index Char : Cmue dcto


Example Programs