Python program to remove characters from odd or even index of a string
Python 3 program to remove characters from odd or even index from a string :
In this example, we will write one program in Python 3 to remove all characters positioned on Even or Odd index. Python string is immutable, i.e. we can’t modify one string directly. e.g. if you want to change the character on index 3, you can’t change it directly like arrays. We need to create one different string if we want to make any modification to a string.
In this tutorial, I will show you how to remove the odd or even index characters from a string in Python. We will ask the user if he wants to remove odd or even indexed characters. The user will also enter the string. Based on the user input, the program will modify the string.
We are going to use the below algorithm in this program :
Algorithm to remove odd or even index character from a string :
1. First, take the string from the user. Save this string in a variable.
2. Create one variable empty string to store the output. As explained above, we can’t change the original string. We will build the string by extracting the required characters from the original string, i.e. if we want to remove all odd indexed characters, we will pick the even indexed characters from the original string and append them to this empty string.
3. Ask the user if he wants to remove odd or even positioned characters from the string. Save this value in a variable.
4. Using a loop, iterate through the string. Add the character of even or odd position to the output string.
5. Print the output string.
Python 3 Program :
input_string = input("Enter a string : ") output_string = "" oddOrEven = int(input("Enter '1' if you want to remove odd positioned characters , '2' for even positioned characters : ")) if oddOrEven ==1 : print ("String after removing characters on odd position : ") for i in range(len(input_string)): if i%2 != 0: output_string = output_string + input_string[i] elif oddOrEven == 2 : print ("String after removing characters on even position : ") for i in range(len(input_string)): if i%2 == 0: output_string = output_string + input_string[i] print (output_string)
Download the above example program from here.
Sample Output :
1. input() method is used here to read the user input. The string that is entered by the user is stored in the input_string variable.
2. output_string is initialized as an empty string. We will append all the required characters to this string.
3. oddOrEven is a flag to decide what type of characters are not required. 1 is for removing the odd positioned characters and 2 is for removing the even positioned characters. input() method is used to read the user input. This method reads the value as a string. int() is used to get the integer representation of the user input value.
4. Using one if-elif condition, we are determining the process to follow. If the value of oddOrEven is 1, the control will enter inside the if case to remove all odd positioned characters. Similarly, if its value is 2, the control will enter inside the elif case to remove all even positioned characters.
5. Using one for loop, we are iterating through the characters of the string one by one and appending the required character to the output_string.
In the above example, we are removing characters from ‘Hello‘. The position of each character are :
H - 1 e - 2 l - 3 l - 4 o - 5
So, the string after removing odd index characters is ‘el’. And the string after removing even index characters is: ‘Hlo‘.
Try to run the example above and drop one comment below if you have any queries.