Python Program to Remove Odd Indexed Characters in a string

In this program, we will write a Python program to remove the odd-indexed characters from a given string.

Problem statement

Given a string, we need to remove all the characters at odd indices and return the resulting string.

Python Program to Remove Odd Indexed Characters in a string

def remove_odd_indexed_chars(string):
    result = ""
    for index in range(len(string)):
        if index % 2 == 0:
            result += string[index]
    return result

# Test the program
input_string = input("Enter a string: ")
result_string = remove_odd_indexed_chars(input_string)
print("Result:", result_string)

How it works

  1. We define a function remove_odd_indexed_chars that takes a string as an input.
  2. We initialize an empty string result to store the characters at even indices.
  3. We iterate over the range of indices of the string using a for loop and the range function.
  4. Inside the loop, we check if the current index is even by using the modulo operator %. If the index is even (i.e., index % 2 == 0), we add the character at that index to the result string using string concatenation.
  5. Finally, we return the result string, which contains only the characters at even indices.

Input / Output

Python Program to Remove Odd Indexed Characters in a string

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else...
In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...