How to use Brute-Force Approach to get even and odd index characters In Python
First, create two separate lists for even and odd characters. Iterate through the given string and then check if the character index is even or odd. Even numbers are always divisible by 2 and odd ones are not. Insert the characters in the created lists and display the lists.
Python3
given_str = 'Geekforgeeks' # given input string even_characters = [] # For storing even characters odd_characters = [] # For storing odd characters for i in range ( len (given_str)): if i % 2 = = 0 : # check if the index is even even_characters.append(given_str[i]) else : odd_characters.append(given_str[i]) # print the odd characters print ( 'Odd characters: {}' . format (odd_characters)) # print the even characters print ( 'Even characters: {}' . format (even_characters)) |
Output:
Gesoges ekfrek
Print Even and Odd Index Characters of a String – Python
Given a string, our task is to print odd and even characters of a string in Python.
Example
Input: w3wiki
Output: Gesoges ekfrek
Contact Us