How to Print alternate characters in a String in Python. [6 Ways]

In this Python article, I will describe how to print alternate characters in a string in Python with different methods and relatable examples.

Keep reading this article to learn the below mentioned methods to print alternate characters in a string in Python.

  • Using string slicing
  • Using for loop
  • Using while loop
  • Using enumerate() function
  • Using list comprehension
  • Using recursion

Python Program to Print Alternate Characters in a String Using String Slicing

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

For Example:

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.

Here is the full code of Python to print alternate characters in a string using string slicing.

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

You can refer to the screenshot below it provides the output of the Python program to print alternate characters in a string using string slicing.

Python program to print alternate characters in a string using string slicing

Note: 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.

Quote = 'Good Things Take Time'

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

Output:

Go hnsTk ie

How to Print Alternate Characters in a String in Python Using For Loop

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

This is the complete source code of Python to print alternate characters in a string using a for loop.

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.”

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

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

Output:

SnFacso

The screenshot below provides the Python program output to print alternate characters in a string.

Print alternate characters in a string using for loop in Python

Print Alternate Characters in a String Using the While Loop

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)

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 in alternate_char.

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

Output:

Cerlt

Refer to the image below to see the Python code output after execution.

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

Python Program to Print Alternate Characters in a String using Enumerate() Function

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

Here is the full code of Python to print alternate characters in a string using the enumerate function.

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.

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

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

Output:

Mthl

You can refer to the image below to see the output of the Python program to print alternate characters in a string using the enumerate function.

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

Print Alternate Characters in a String in Python using 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.

Here is the complete source code of Python to print alternate characters in a string using list comprehension.

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 atlernate_char using list comprehension in Python. It iterates through the string characters starting from index 0 with a step size 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)

Python Program to Print Alternate Characters in a String Using Recursion

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.

This is the source code of Python to print alternate characters in a string using recursion.

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.

Refer to the image below to see the program’s output to print alternate characters in a string using recursion in Python.

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

Conclusion

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 for loop, using the 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: