How to Split, Reverse, and Join Strings in Python
This guide explains a common string manipulation task in Python: splitting a string into a list of words, reversing the order of those words, and then joining them back into a single string. We'll focus on the most efficient and Pythonic approach using str.split()
, slicing for reversal, and str.join()
.
Splitting, Reversing, and Joining a String
Here's the most concise and efficient way to split, reverse, and rejoin a string in Python:
my_str = 'tutorial reference com'
my_list = my_str.split(' ') # 1. Split into a list of words
print(my_list) # Output: ['tutorial', 'reference', 'com']
reversed_list = my_list[::-1] # 2. Reverse using slicing.
print(reversed_list) # Output: ['com', 'reference', 'tutorial']
my_str_again = ' '.join(reversed_list) # 3. Join back into a string
print(my_str_again) # Output: com reference tutorial
- You can also reverse the list in place using the
reverse()
method. - This example uses spaces as the delimiter, however you can use other delimiters.
Understanding the Steps
Let's break down each step in detail:
Splitting the String (str.split()
):
my_str = 'tutorial reference com'
my_list = my_str.split(' ') # Split on spaces
print(my_list) # Output: ['tutorial', 'reference', 'com']
# Example with a different delimiter:
my_str_comma = 'tutorial,reference,com'
my_list_comma = my_str_comma.split(',') # Split on commas
print(my_list_comma) # Output: ['tutorial', 'reference', 'com']
my_str.split(' ')
: Thesplit()
method divides the string into a list of substrings. The argument tosplit()
is the delimiter. If you don't provide an argument, it splits on whitespace (spaces, tabs, newlines).
Reversing the List (Slicing)
The most efficient way to reverse a list without modifying the original is to use slicing with a step of -1
:
my_list = ['tutorial', 'reference', 'com']
reversed_list = my_list[::-1] # Reverse using slicing
print(reversed_list) # Output:['com', 'reference', 'tutorial']
print(my_list) # Output:['tutorial', 'reference', 'com']
my_list[::-1]
: Creates a reversed copy of the list. The originalmy_list
is unchanged.- The
[::-1]
slice creates a reversed copy of the list. - If you want to reverse the list in place using
my_list.reverse()
, you can do that as well, but that will change themy_list
variable:
my_list = ['tutorial', 'reference', 'com']
my_list.reverse() # Reverse IN PLACE
print(my_list) # Output: ['com', 'reference', 'tutorial']
Joining the List (str.join()
):
my_list = ['tutorial', 'reference', 'com']
my_list.reverse()
print(my_list) # Output: ['com', 'reference', 'tutorial']
my_str_again = ' '.join(my_list)
print(my_str_again) # Output: com reference tutorial
my_str_again = ','.join(my_list)
print(my_str_again) # Output: com,reference,tutorial
my_str_again = ''.join(my_list)
print(my_str_again) # Output: comreferencetutorial
' '.join(reversed_list)
: Thejoin()
method takes an iterable (like a list) of strings and concatenates them into a single string. The string you calljoin()
on (in this case, a space:' '
) is used as the separator between the elements.- You can use any separator you want (e.g.,
','
,'-'
, or even''
for no separator).