How to split a string, reverse it and join it back in Python? I have an idea for this topic as follows: perform a string split using the str.split()
function, reverse it with the reverse()
function, or use slice and then join the part element with the join()
function. Detailed ideas are in the article below.
Split a string, reverse it and join it back in Python
Use the str.split()
function in combination with the reverse()
and join()
functions
Syntax:
Functions | Parameters |
str.split() The str.split function returns a list of strings after splitting these strings based on the given delimiters. | |
reversed() The reverse() function is used to reverse the original object. | |
str.join(object) The join() function returns an object that is a character string of the elements of the specified list or tuple joined together by a delimiter. | str: Delimiter for string concatenation. object: List or tuple containing elements that are strings to be concatenated. |
Example:
- I perform string splitting using the
str.split()
function, which returns a list of strings after splitting. - Get that list reversed using the
reverse()
method. - Finally, use the
join()
function to concatenate the elements into a new string.
strIntro = 'Visit Learnshareit website' # The str.split() function returns a list of strings after splitting myList = strIntro.split() print(myList) # Use the reverse() method to reverse the list myList.reverse() print(myList) # Join the elements after being reversed using the join() function newStr = ' '.join(myList) print(f'The newly generated string is: {newStr}')
Output:
['Visit', 'Learnshareit', 'website']
['website', 'Learnshareit', 'Visit']
The newly generated string is: website Learnshareit Visit
Use the str.split function to combine the list slice and the join() function
Syntax:
org_list [ start_index : end_index : step]
Parameters:
- start: is the starting position to get the value. If left blank, the default is to get from the beginning of the tuple.
- end: end position. If left blank, all values in the tuple will be taken.
Example:
- I perform string splitting using the
str.split()
function, which returns a list of strings after splitting. - To reverse the list by the slice, set step = -1. The slice function will take the element from the list’s right to left.
- Finally, use the
join()
function to concatenate the elements into a new string.
strIntro = 'Visit Learnshareit website' # The str.split() function returns a list of strings after splitting myList = strIntro.split() print(myList) # Reverse the list by the slice # Step = -1 represents getting the list element from right to left reverse = myList[::-1] print(reverse) # Join elements using the join() function result = ' '.join(reverse) print(f'The newly generated string is: {result}')
Output:
['Visit', 'Learnshareit', 'website']
['website', 'Learnshareit', 'Visit']
The newly generated string is: website Learnshareit Visit
Summary
The topic split a string, reverse it and join it back in Python is not too complicated. Hope you get an idea for this topic after reading the article. If you have any questions, please leave a comment below! Thanks for reading.
Maybe you are interested:
- Split a string and get the last element in Python
- Split a string into fixed size chunks in Python
- Split string by space and preserve quoted strings in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java