Python Program for Printing Odd and Even Letters of a String

Python Program for Printing Odd and Even Letters of a String

Strings in Python:

String in Python. The string is an immutable sequence data type in Python. It is a string of Unicode letters surrounded by single, double, or triple quotations… This is the second string in the Multi-line format. If a string literal must embed double quotations as part of a string, it must be enclosed in single quotes.

Given the string, the task is to print even and odd index characters of the given string in Python.

Examples:

Example1:

Input:

The given string = BtechGeeks

Output:

The odd characters in given string =  Behek
The even characters in given string = tcGes

Example2:

Input:

The given string = uploadbutton

Output:

The odd characters in given string = ulabto
The even characters in given string = podutn

Python Program for Printing Odd and Even Letters of a String

Below are the ways to write a Python Program to print Odd and Even Letters of the given String.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Method #1: Using For Loop and String Concatenation (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take two empty strings(say oddChars and evenChars) and initialize them with a null string using “”.
  • Calculate the length of the string using the len() function and store it in a variable.
  • Loop till the length of the string using For loop.
  • Check if the iterator value is divisible by 2 or not using the If statement.
  • If it is true then concatenate the string iterator value to the oddChars string.
  • Else concatenate the string iterator value to the evenChars string.
  • Print the oddChars string.
  • Print the evenChars string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
givenstring = 'BtechGeeks'
# Take two empty strings(say oddChars and evenChars)
# and initialize them with a null string using "".
evenChars = ""
oddChars = ""
# Calculate the length of the string using the len() function and store it in a variable.
strnglen = len(givenstring)
# Loop till the length of the string using For loop.
for itrvalue in range(strnglen):
    # Check if the iterator value is divisible by 2 or not using the If statement.
    #
    if itrvalue % 2 == 0:
        # If it is true then concatenate the string iterator value to the oddChars string
        oddChars = oddChars + givenstring[itrvalue]
    else:
        # Else concatenate the string iterator value to the evenChars string.
        evenChars = evenChars + givenstring[itrvalue]
print('The given string = ', givenstring)
# Print the oddChars string.
print('The odd characters in given string = ', oddChars)
# Print the evenChars string.
print('The even characters in given string =', evenChars)

Output:

The given string =  BtechGeeks
The odd characters in given string =  Behek
The even characters in given string = tcGes

Method #2: Using For Loop and String Concatenation (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take two empty strings(say oddChars and evenChars) and initialize them with a null string using “”.
  • Calculate the length of the string using the len() function and store it in a variable.
  • Loop till the length of the string using For loop.
  • Check if the iterator value is divisible by 2 or not using the If statement.
  • If it is true then concatenate the string iterator value to the oddChars string.
  • Else concatenate the string iterator value to the evenChars string.
  • Print the oddChars string.
  • Print the evenChars string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
givenstring = input('Enter some random string = ')
# Take two empty strings(say oddChars and evenChars)
# and initialize them with a null string using "".
evenChars = ""
oddChars = ""
# Calculate the length of the string using the len() function and store it in a variable.
strnglen = len(givenstring)
# Loop till the length of the string using For loop.
for itrvalue in range(strnglen):
    # Check if the iterator value is divisible by 2 or not using the If statement.
    #
    if itrvalue % 2 == 0:
        # If it is true then concatenate the string iterator value to the oddChars string
        oddChars = oddChars + givenstring[itrvalue]
    else:
        # Else concatenate the string iterator value to the evenChars string.
        evenChars = evenChars + givenstring[itrvalue]
print('The given string = ', givenstring)
# Print the oddChars string.
print('The odd characters in given string = ', oddChars)
# Print the evenChars string.
print('The even characters in given string =', evenChars)

Output:

Enter some random string = uploadbutton
The given string = uploadbutton
The odd characters in given string = ulabto
The even characters in given string = podutn

Related Programs: