Print the even and odd index characters of a string in python

In this post, we will learn how to print the characters of the even and odd indices of a string in Python. With this program, you will learn how to check if a value is even or odd, how to iterate through a string in Python and how to read the characters of a string in Python.

This program will take one string as input from the user and it will print all the characters at even and odd positions separately.

What are even and odd values:

A number is called even if it is perfectly divisible by 2. That means, if we are dividing that number by 2, the remainder will be 0. For example, 4 is an even number.

We can use the modulo operator, %, to check if a number is even or odd in Python. The modulo operator can be used to find the remainder of a division operation. The x%y operation will return the remainder of x/y. For any number n, n%2 will be always 0 if n is even.

Similarly, a number is called odd if it is not perfectly divisible by 2. If we divide the number by 2, the remainder will be 1 for an odd number. For example, 11 is an odd number. We can use the modulo operator to find out if a number is odd or not. The result of n%2 will be 1 for any odd number n.

Algorithm:

We can iterate over the indices of a string. The character at any index i of a string s can be accessed with square brackets like s[i]. We can use the below algorithm to get the characters of a string at odd and even index positions:

  • Create two empty arrays to hold the even and odd index characters.
  • Take the string as input from the user.
  • Iterate through the characters of the string one by one.
  • For each character, check if the index is even or odd. If it is even, insert it to the array of even index characters, else insert it to the array of odd index characters.

Method 1: Python program to print the odd and even index characters of a string:

Below is the complete Python program:

given_string = input('Enter a string: ')

even_chars = []
odd_chars = []

for i in range(len(given_string)):
    if i % 2 == 0:
        even_chars.append(given_string[i])
    else:
        odd_chars.append(given_string[i])

print(f'Odd characters: {odd_chars}')
print(f'Even characters: {even_chars}')

Download it on Github

Here,

  • The given_string variable is used to assign the user input string. We are using the input() method to read this string as input from the user and assign it to the given_string variable.
  • The variables even_chars and odd_chars are arrays to hold the even and odd index characters. These are initialized as empty arrays.
  • The for loop is iterating through the characters of the string one by one. The len() method returns the length of the string given_string and range(len(given_string)) returns a sequence of numbers from 0 to string length(exclusive).
    • It appends the character to the even_chars array if the current index i is even. Else, it appends it to the odd_chars array.
  • The last two lines are printing the content of the arrays odd_chars and even_chars, i.e. all odd and even characters.

Python example print even odd index characters of a string

Sample Output:

If you run this program, it will print outputs as below:

Enter a string: hello
Odd characters: ['e', 'l']
Even characters: ['h', 'l', 'o']

Enter a string: lazyfox
Odd characters: ['a', 'y', 'o']
Even characters: ['l', 'z', 'f', 'x']

Method 2: How to add the characters to a string:

We can also add the characters to an empty string instead of using arrays. This will create two strings holding the even and odd index characters. For example,

given_string = input('Enter a string: ')

even_chars = ''
odd_chars = ''

for i in range(len(given_string)):
    if i % 2 == 0:
        even_chars += given_string[i]
    else:
        odd_chars += given_string[i]

print(f'Odd characters: {odd_chars}')
print(f'Even characters: {even_chars}')

Download it on Github

Here, we have initialized two empty strings even_chars and odd_chars to hold the characters at even and odd indices of the string. The for loop is appending the characters to these strings and the last two print statements are printing the values of these strings.

It will print outputs as below:

Enter a string: hello
Odd characters: el
Even characters: hlo

You might also like: