Strings in Python:
A string is a series of one or more characters (letters, integers, symbols) that might be constant or variable. Strings, which are made up of Unicode, are immutable sequences, which means they do not change.
Because the text is such a frequent type of data in our daily lives, the string data type is a critical building block of programming.
Given a string, the task is to remove all the characters present at odd indices in the given string in Python
- Python Program to Accept a Hyphen Separated Sequence of Words as Input and Print the Words in a Hyphen-Separated Sequence after Sorting them Alphabetically
- 5 Simple Ways to Add a Newline Character in Python
- Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively
Examples:
Example1:
Input:
given string = HelloThisisBTechgeeks
Output:
The given string before modification = HelloThisisBTechgeeks The given string after modification = HlohssTcges
Example2:
Input:
given string = btechgeeks online coding platform for coding studeents
Output:
Enter the given random string = btechgeeks online coding platform for coding studeents The given string before modification = btechgeeks online coding platform for coding studeents The given string after modification = behek niecdn ltomfrcdn tdet
Program to Remove the Characters of Odd Index Values in a String in Python
There are several ways to remove all the characters present at odd indices in the given string in Python some of them are:
- Using Modulus operator and String Concatenation(Static Input)
- Using Modulus operator and String Concatenation(User Input)
- Using List Comprehension and Join function(Static Input)
- Using List Comprehension and Join function(User Input)
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Method #1)Using Modulus operator and String Concatenation(Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Create a function say removeOddString that accepts the string as an argument and removes all the characters present at odd indices in the given string.
- Pass the given string as an argument to the removeOddString function.
- In the function take an empty string say tempstng using ”.
- Traverse the given string using For loop.
- Use an if statement and modulus operator to determine whether the string’s index is odd or even.
- If the index is an even number, append the character to the tempstng using String Concatenation.
- Return the tempstng.
- Print the modified string which is returned from the function.
- The Exit of the program.
Below is the implementation:
# function that accepts the string as an argument # and removes all the characters present at odd indices in the given string. def removeOddString(givenstrng): # In the function take an empty string say tempstng using '' . tempstng = "" # Traverse the given string using For loop. for charindex in range(len(givenstrng)): # Use an if statement and modulus operator to # determine whether the string's index is odd or even. if charindex % 2 == 0: # If the index is an even number, append the # character to the tempstng using String Concatenation. tempstng = tempstng + givenstrng[charindex] # Return the tempstng. return tempstng # Give the string as static input and store it in a variable. givenstrng = 'HelloThisisBTechgeeks' # print the given string without modification' print('The given string before modification = ', givenstrng) # Pass the given string as an argument to the removeOddString function. resstrng = removeOddString(givenstrng) # print the given string after modification print('The given string after modification = ', resstrng)
Output:
The given string before modification = HelloThisisBTechgeeks The given string after modification = HlohssTcges
Method #2)Using Modulus operator and String Concatenation(User Input)
Approach:
- Give the string as the user input with the help of the input() function and store it in a variable.
- Create a function say removeOddString that accepts the string as an argument and removes all the characters present at odd indices in the given string.
- Pass the given string as an argument to the removeOddString function.
- In the function take an empty string say tempstng using ”.
- Traverse the given string using For loop.
- Use an if statement and modulus operator to determine whether the string’s index is odd or even.
- If the index is an even number, append the character to the tempstng using String Concatenation.
- Return the tempstng.
- Print the modified string which is returned from the function.
- The Exit of the program.
Below is the implementation:
# function that accepts the string as an argument # and removes all the characters present at odd indices in the given string. def removeOddString(givenstrng): # In the function take an empty string say tempstng using '' . tempstng = "" # Traverse the given string using For loop. for charindex in range(len(givenstrng)): # Use an if statement and modulus operator to # determine whether the string's index is odd or even. if charindex % 2 == 0: # If the index is an even number, append the # character to the tempstng using String Concatenation. tempstng = tempstng + givenstrng[charindex] # Return the tempstng. return tempstng # Give the string as the user input with the help of input() function and store it in a variable. givenstrng = input('Enter the given random string = ') # print the given string without modification' print('The given string before modification = ', givenstrng) # Pass the given string as an argument to the removeOddString function. resstrng = removeOddString(givenstrng) # print the given string after modification print('The given string after modification = ', resstrng)
Output:
Enter the given random string = btechgeeks online coding platform for coding studeents The given string before modification = btechgeeks online coding platform for coding studeents The given string after modification = behek niecdn ltomfrcdn tdet
Explanation:
- A string must be entered by the user and saved in a variable.
- This string is provided to a function as an argument.
- A variable is initialized with an empty character in the function.
- To iterate through the string, a for loop is needed.
- An if statement determines whether the string’s index is odd or even.
- If the index is even, the empty character variable is appended to the string, eliminating the character indirectly.
- Finally, the updated string is printed.
Method #3:Using List Comprehension and Join function(Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Create a function say removeOddString that accepts the string as an argument and removes all the characters present at odd indices in the given string.
- Pass the given string as an argument to the removeOddString function.
- Convert the given string into a list of characters using the list() function.
- Using list comprehension with the help of the if and modulus operator eliminates the odd index characters.
- Join the list into the string using the join() function.
- Return the modified string.
- Print the modified string which is returned from the function.
- The Exit of the program.
Below is the implementation:
# function that accepts the string as an argument # and removes all the characters present at odd indices in the given string. def removeOddString(givenstrng): # Convert the given string into a list of characters using the list() function. liststrng = list(givenstrng) # Using list comprehension with the help of the if and modulus operator eliminates the odd index characters. liststrng = [givenstrng[inde] for inde in range(len(givenstrng)) if inde % 2 == 0] # Join the list into the string using the join() ffunction. tempstng = ''.join(liststrng) # Return the tempstng. return tempstng # Give the string as static input and store it in a variable. givenstrng = 'Hello this is BTechGeeks online coding platform for coding students' # print the given string without modification' print('The given string before modification = ', givenstrng) # Pass the given string as an argument to the removeOddString function. resstrng = removeOddString(givenstrng) # print the given string after modification print('The given string after modification = ', resstrng)
Output:
The given string before modification = Hello this is BTechGeeks online coding platform for coding students The given string after modification = Hloti sBehek niecdn ltomfrcdn tdns
Method #4:Using List Comprehension and Join function(User Input)
Approach:
- Give the string as the user input with the help of the input() function and store it in a variable.
- Create a function say removeOddString that accepts the string as an argument and removes all the characters present at odd indices in the given string.
- Pass the given string as an argument to the removeOddString function.
- Convert the given string into a list of characters using the list() function.
- Using list comprehension with the help of the if and modulus operator eliminates the odd index characters.
- Join the list into the string using the join() function.
- Return the modified string.
- Print the modified string which is returned from the function.
- The Exit of the program.
Below is the implementation:
# function that accepts the string as an argument # and removes all the characters present at odd indices in the given string. def removeOddString(givenstrng): # Convert the given string into a list of characters using the list() function. liststrng = list(givenstrng) # Using list comprehension with the help of the if and modulus operator eliminates the odd index characters. liststrng = [givenstrng[inde] for inde in range(len(givenstrng)) if inde % 2 == 0] # Join the list into the string using the join() ffunction. tempstng = ''.join(liststrng) # Return the tempstng. return tempstng # Give the string as the user input with the help of input() function and store it in a variable. givenstrng = input('Enter the given random string = ') # print the given string without modification' print('The given string before modification = ', givenstrng) # Pass the given string as an argument to the removeOddString function. resstrng = removeOddString(givenstrng) # print the given string after modification print('The given string after modification = ', resstrng)
Output:
Enter the given random string = good morning this is btechgeeks The given string before modification = good morning this is btechgeeks The given string after modification = go onn hsi tcges
Related Programs:
- Python Program to Remove the ith Occurrence of the Given Word in a List where Words can Repeat
- Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions
- Python Program to Take in a String and Replace Every Blank Space with Hyphen
- Python Program to Take in the Marks of 5 Subjects and Display the Grade