Write a Python program to Remove Odd Index Characters in a String with a practical example.
Python Program to Remove Odd Index Characters in a String Example
This python program allows the user to enter a string. First, we used For Loop to iterate each character in a String. Inside the For Loop, we used the If statement to check whether the index value is divisible by true or not. If True, add that character to str2 string. Please refer String article to know about Python Strings
# Python Program to Remove Odd Index Characters in a String str1 = input("Please Enter your Own String : ") str2 = '' for i in range(len(str1)): if(i % 2 == 0): str2 = str2 + str1[i] print("Original String : ", str1) print("Final String : ", str2)
Python remove odd Index Characters in a String output
Please Enter your Own String : Tutorial Gateway
Original String : Tutorial Gateway
Final String : Ttra aea
Program to Remove Odd Index Characters in a String Example 2
This program program to remove odd index characters is the same as above. However, we just replaced the For Loop with Python While Loop.
# Python program to Remove Odd Index Characters in a String str1 = input("Please Enter your Own String : ") str2 = '' i = 0 while(i < len(str1)): if(i % 2 == 0): str2 = str2 + str1[i] i = i + 1 print("Original String : ", str1) print("Final String : ", str2)
Python remove odd Index characters in a string output
Please Enter your Own String : Python Programs
Original String : Python Programs
Final String : Pto rgas
Program to Remove String Characters at Odd Index Example 3
The remove odd index Characters in a Python string is the same as the first example. However, this time we used Python Functions to separate the logic.
# Python program to Remove Odd Index Characters in a String def newString(str1): str2 = '' for i in range(len(str1)): if(i % 2 == 0): str2 = str2 + str1[i] return str2 string = input("Please Enter your Own String : ") print("Original String : ", string) print("Final String : ", newString(string))