Learn Programming and technology with ease @ developerpublish.com

HomePythonPython Program to Remove Odd Indexed Characters in a string

# 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

### You May Also Like

#### Python Program to Remove Duplicates from a Linked List

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...

#### Python Program to Solve the Celebrity Problem

This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...

#### Python Program to Solve n-Queen Problem with Recursion

This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...