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

avatar

Borislav Hadzhiev

Last updated: Jun 25, 2022

banner

Photo from Unsplash

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 = 'one two three four' my_list = my_str.split(' ') print(my_list) # 👉️ ['one', 'two', 'three', 'four'] my_list.reverse() print(my_list) # 👉️ ['four', 'three', 'two', 'one'] my_str_again = ' '.join(my_list) print(my_str_again) # 👉️ four three two one

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

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

main.py
my_str = 'one two three four' my_list = my_str.split(' ') print(my_list) # 👉️ ['one', 'two', 'three', 'four']

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 = 'one two three four' my_list = my_str.split(' ') print(my_list) # 👉️ ['one', 'two', 'three', 'four'] my_list.reverse() print(my_list) # 👉️ ['four', 'three', 'two', 'one']

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

The method returns None because it mutates the original list.

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 = 'one two three four' my_list = my_str.split(' ') print(my_list) # 👉️ ['one', 'two', 'three', 'four'] reversed_list = my_list[::-1] print(reversed_list) # 👉️ ['four', 'three', 'two', 'one'] print(my_list) # 👉️ ['one', 'two', 'three', 'four']

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

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

main.py
my_str = 'one two three four' my_list = my_str.split(' ') print(my_list) # 👉️ ['one', 'two', 'three', 'four'] my_list.reverse() print(my_list) # 👉️ ['four', 'three', 'two', 'one'] my_str_again = ' '.join(my_list) print(my_str_again) # 👉️ four three two one

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 = 'one two three four' my_list = my_str.split(' ') print(my_list) # 👉️ ['one', 'two', 'three', 'four'] my_list.reverse() print(my_list) # 👉️ ['four', 'three', 'two', 'one'] my_str_again = ','.join(my_list) print(my_str_again) # 👉️ four,three,two,one

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 = 'one two three four' my_list = my_str.split(' ') print(my_list) # 👉️ ['one', 'two', 'three', 'four'] my_list.reverse() print(my_list) # 👉️ ['four', 'three', 'two', 'one'] my_str_again = ''.join(my_list) print(my_str_again) # 👉️ fourthreetwoone
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.