- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python Program to Remove the Characters of Odd Index Values in a String
When it is required to remove characters from odd indices of a string, a method is defined that takes the string as parameter.
Below is the demonstration of the same −
def remove_odd_index_characters(my_str): new_string = "" i = 0 while i < len(my_str): if (i % 2 == 1): i+= 1 continue new_string += my_str[i] i+= 1 return new_string if __name__ == '__main__': my_string = "Hi there Will" my_string = remove_odd_index_characters(my_string) print("Characters from odd index have been removed") print("The remaining characters are : ") print(my_string)
Characters from odd index have been removed The remaining characters are : H hr il
A method named ‘remove_odd_index_characters’ is defined, that takes a string as parameter.
An empty string is created.
The string is iterated over, and the index of every element is divided by 2.
If the remainder is not 0, then it is considered as an odd index, and it is deleted.
In the main method, the method is called, and the string is defined.
This string is passed as a parameter to the method.
The output is displayed on the console.
- Related Articles
- Program to remove duplicate characters from a given string in Python
- Python program to get characters from the string using the index
- Program to remove string characters which have occurred before in Python
- Python program to find the sum of Characters ascii values in String List
- How to remove a list of characters in string in Python?
- Python Program to Remove the nth Index Character from a Non-Empty String
- C# program to remove a range of characters by index using StringBuilder
- C# program to remove characters starting at a particular index in StringBuilder
- How to remove specific characters from a string in Python?
- Golang Program to get characters from a string using the index
- C++ Program to Remove all Characters in a String Except Alphabets
- C# Program to remove duplicate characters from String
- Python Program to get the index of the substring in a string
- How to remove characters except digits from string in Python?
- PHP program to remove non-alphanumeric characters from string