Last updated: Apr 8, 2024
Reading timeยท3 min
To split a string, reverse it and join it back:
str.split()
method to split the string into a list.list.reverse()
method to reverse the elements of the list.str.join()
method to join the list into a string.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 first step is to use the str.split()
method to split the string into a
list.
Here is an example that uses a comma as the separator.
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.
my_str = 'bobby hadz com' my_list = my_str.split(' ') print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
The next step is to reverse the elements of the list using the list.reverse()
method.
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.
If you don't want to mutate the original list in place, use the
list slicing syntax with a step
of -1
.
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']
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.
The last step is to use the str.join()
method to join the reversed list into a
string.
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.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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: