Get Alternate Letters from String in Python

As a developer, I was working on a project where I needed to extract every alternate character from a string.

At first, I thought Python might have a built-in function for this. But as I explored, I realized there are multiple simple ways to achieve it.

In this tutorial, I’ll show you five easy methods to get alternate letters from string in Python. These methods are practical, and I’ve personally used them in real-world projects.

Method 1: Use Python String Slicing

Python string slicing is a feature that allows you to access and manipulate specific parts of a string by slicing it from start to end.

string = 'Keep up the good work'
print(string[0:8])                                     // prints 'Keep up'

We can use the step parameter in the string slicing to print the alternate characters in a string in Python.

Quote = 'Good Things Take Time'

result = Quote[0::4]
print(result)

This slicing notation starts from index 0 and extracts every 4th alternate character of the given string in Python, as we have provided the step parameter as 4.

result = Quote[0::4]

Output:

Good Things Take Time
G nT e

I executed the above example code and added the screenshot below.

Python program to print alternate characters in a string using string slicing
Quote = 'Good Things Take Time'

result = Quote[::2]
print(result)

Output:

Go hnsTk ie

We can provide 2 instead of 4 in the step parameter of the string slicing to get the alternate characters of a string in Python.

Method 2: Use a For Loop in Python

A for loop in Python is used to iterate over a sequence or collection. With the for loop, we can execute a set of statements once for each item in a list, tuple, set, etc in Python.

USA_state ="San Francisco"
alternate_char=""
odd=True
for c in USA_state:
  if odd:
   alternate_char = alternate_char + c
  else:
    alternate_char = alternate_char
  odd = not odd
print(alternate_char)

Here, I have used odd = True, indicating that the first character to be appended will be considered “odd.”

odd=True
for c in USA_state:
  if odd:
   alternate_char = alternate_char + c
  else:
    alternate_char = alternate_char
  odd = not odd

Output:

SnFacso

I executed the above example code and added the screenshot below.

Print alternate characters in a string using for loop in Python

Inside the for loop, we check using a conditional statement. If it is True, it means the current character is odd and is appended to the string alternate_string. If it is False, no action will be taken.

Method 3: Use the While Loop in Python

The while loop in Python executes a set of statements as long as the condition is True. Here is the Python source code to check the Armstrong number in Python using a while loop.

Car = 'Chevrolet'
i = 0
alternate_char = ''

while i < len(Car):
    alternate_char += Car[i]
    i += 2

print(alternate_char)
while i < len(Car):
    alternate_char += Car[i]
    i += 2

Output:

Cerlt

I executed the above example code and added the screenshot below.

How to print alternate characters in a string using while loop in Python

In this while loop, I want to append the characters at index i to alternate_char and increment i by 2 in each iteration so that it will append only alternate characters to alternate_char.

Method 4: Use Python Enumerate() Function

Enumerate is a built-in function in Python that adds a counter to an iterable and returns it as an enumerate object (an iterator with index and value).

Name = 'Mitchell'
name_alternate_char = []
for index, value in enumerate(Name):
      if index % 2 == 0:
         name_alternate_char.append(value)
name_alternate_char = "".join(name_alternate_char)
print(name_alternate_char)

I have used the enumerate function in Python to retrieve both the index and value of each character, and the append function is used to append the value to the list name_alternate_char.

for index, value in enumerate(Name):
      if index % 2 == 0:
         name_alternate_char.append(value)
name_alternate_char = "".join(name_alternate_char)

Output:

Mthl

I executed the above example code and added the screenshot below.

Program to print alternate characters in a string using enumerate function in Python

After iterating over all the characters of a string, the join function in Python is used to join all the characters stored into a single string.

Method 5: Use Python List Comprehension

List comprehension in Python offers a shorter syntax when we want to create a new list based on the values of an existing list.

def print_alternate(string):
    alternate_char = [string[i] for i in range(0, len(string), 2)]
    result = ''.join(alternate_char)
    print(result)

News = "Cyber Attacks Hit Americans"
print_alternate(News)

It creates a list called alternate_char using list comprehension in Python. It iterates through the string characters starting from index 0 with a step size of 2.

Then, to join characters in the alternate_char together in a string, use the join() function in Python.

alternate_char = [string[i] for i in range(0, len(string), 2)]
    result = ''.join(alternate_char)

Method 6: Use Recursion in Python

In Python, recursion is a method used to break a problem down into smaller and smaller sub-problems until you get to a small enough problem that can be solved.

def print_alternate(string, index=0):
    if index < len(string):
        print(string[index], end="")
        print_alternate(string, index + 2)
 
Quote = "California, the place where dreams come true."
print_alternate(Quote)

Output:

Clfri,tepaeweedem oetu.

I executed the above example code and added the screenshot below.

How to print alternate characters in a string using recursion in Python

Recursion can be used to process strings by repeatedly calling the function, such as printing alternate characters until the end of the string.

I hope you find this Python article helpful. Here, I have explained the six methods to print alternate characters in a string in Python, such as using string slicing, using a for loop, using a while loop, using the enumerate() function, using list comprehension, and using recursion.

Choosing the right method or condition purely depends on your requirement to print alternate characters in a string in Python.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.