Split a String, Reverse it and Join it back in Python

avatar
Borislav Hadzhiev

Last updated: Feb 19, 2023
3 min

banner

# Split a string, reverse it and join it back in Python

To split a string, reverse it and join it back:

  1. Use the str.split() method to split the string into a list.
  2. Use the list.reverse() method to reverse the elements of the list.
  3. Use the str.join() method to join the list into a string.
main.py
my_str = 'bobby hadz com' my_list = my_str.split(' ') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] my_list.reverse() print(my_list) # ๐Ÿ‘‰๏ธ ['com', 'hadz', 'bobby'] my_str_again = ' '.join(my_list) print(my_str_again) # ๐Ÿ‘‰๏ธ com hadz bobby

split string reverse it and join it back

The first step is to use the str.split() method to split the string into a list.

We used a space for the separator but you can use any other separator (e.g. a comma or a hyphen).

Here is an example that uses a comma as the separator.

main.py
my_str = 'bobby,hadz,com' my_list = my_str.split(',') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] my_list.reverse() print(my_list) # ๐Ÿ‘‰๏ธ ['com', 'hadz', 'bobby'] my_str_again = ','.join(my_list) print(my_str_again) # ๐Ÿ‘‰๏ธ com,hadz,bobby

The str.split() method splits the string into a list of substrings using a delimiter.

main.py
my_str = 'bobby hadz com' my_list = my_str.split(' ') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

The next step is to reverse the elements of the list using the list.reverse() method.

main.py
my_str = 'bobby hadz com' my_list = my_str.split(' ') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] my_list.reverse() print(my_list) # ๐Ÿ‘‰๏ธ ['com', 'hadz', 'bobby']

The list.reverse() method reverses the elements of the list in place.

The method returns None because it mutates the original list.

# Reversing the list without a mutation

If you don't want to mutate the original list in place, use the list slicing syntax with a step of -1.

main.py
my_str = 'bobby hadz com' my_list = my_str.split(' ') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] reversed_list = my_list[::-1] print(reversed_list) # ๐Ÿ‘‰๏ธ ['com', 'hadz', 'bobby'] print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

reverse list without mutation

The syntax for list slicing is a_list[start:stop:step].

We didn't specify a value for start and stop, but specified -1 for step to get a new, reversed list.

# Joining the reversed list

The last step is to use the str.join() method to join the reversed list into a string.

main.py
my_str = 'bobby hadz com' my_list = my_str.split(' ') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] my_list.reverse() print(my_list) # ๐Ÿ‘‰๏ธ ['com', 'hadz', 'bobby'] my_str_again = ' '.join(my_list) print(my_str_again) # ๐Ÿ‘‰๏ธ com hadz bobby

joining the reversed list

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

We joined the list with a space separator, but you can use any other separator.

main.py
my_str = 'bobby hadz com' my_list = my_str.split(' ') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] my_list.reverse() print(my_list) # ๐Ÿ‘‰๏ธ ['com', 'hadz', 'bobby'] my_str_again = ','.join(my_list) print(my_str_again) # ๐Ÿ‘‰๏ธ com,hadz,bobby

If you don't need a separator and just want to join the list's elements into a string, call the join() method on an empty string.

main.py
my_str = 'bobby hadz com' my_list = my_str.split(' ') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] my_list.reverse() print(my_list) # ๐Ÿ‘‰๏ธ ['com', 'hadz', 'bobby'] my_str_again = ''.join(my_list) print(my_str_again) # ๐Ÿ‘‰๏ธ comhadzbobby

When the join() method is called on an empty string, the list's elements are joined without a separator between them.

I've also written an article on how to iterate over a list or string in reverse order.

# 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