Print alternate characters in a String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
2 min

banner

# Table of Contents

  1. Print alternate characters in a String in Python
  2. Print alternate characters in a String using enumerate()

# Print alternate characters in a String in Python

Use string slicing to print the alternate characters in a string, e.g. print(string[::2]).

The slice will contain every other character of the original string.

main.py
my_str = 'bobbyhadz' result = my_str[::2] print(result) # ๐Ÿ‘‰๏ธ bbyaz result = my_str[1::2] print(result) # ๐Ÿ‘‰๏ธ obhd

print alternate characters in string

The code for this article is available on GitHub

We used string slicing to print the alternate characters in a string.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The slice my_str[::2] starts at index 0 and goes to the end of the string selecting every second character.

main.py
my_str = 'bobbyhadz' result = my_str[::2] print(result) # ๐Ÿ‘‰๏ธ bbyaz result = my_str[1::2] print(result) # ๐Ÿ‘‰๏ธ obhd

The slice my_str[1::2] starts at index 1 and goes to the end of the string selecting every second character.

# Print alternate characters in a String using enumerate()

A more manual approach would be to use the enumerate() function to get access to the index of the current iteration.

main.py
my_str = 'bobbyhadz' result = '' for index, char in enumerate(my_str): if index % 2 == 0: result += char print(result) # ๐Ÿ‘‰๏ธ bbyaz

print alternate characters in string using enumerate

The code for this article is available on GitHub

The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py
for index, char in enumerate('abc'): print(index, char) # ๐Ÿ‘‰๏ธ 0 a, 1 b, 2 c

On each iteration, we use the modulo % operator to check if the remainder of dividing the index by 2 equals 0.

The modulo (%) operator returns the remainder from the division of the first value by the second.

main.py
print(10 % 2) # ๐Ÿ‘‰๏ธ 0 print(10 % 4) # ๐Ÿ‘‰๏ธ 2

If the index is an even number, we add the character to a new string.

main.py
my_str = 'bobbyhadz' result = '' for index, char in enumerate(my_str): if index % 2 == 0: result += char print(result) # ๐Ÿ‘‰๏ธ bbyaz
The code for this article is available on GitHub

The += operator is a shorthand for result = result + char.

Which approach you pick is a matter of personal preference. I'd go with using string slicing as it is quite readable and more concise.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev