In this tutorial, we’ll learn how to easily reverse string in Python. We’ll going through multiple examples to understand how to practically reverse string in Python.
Outline
- Introduction: Reverse String In Python
- Implementing Reverse String In Python (3 Examples)
- Example 1: Reverse a Simple String
- Example 2: Reverse a List of Strings in Python For Loop
- Example 3: Reverse a List of Strings in Python While Loop
- Conclusion
Introduction: Reverse String In Python
As the name suggests, it means reversing the order of characters in a Python string. For instance, we’ve a string ‘Python’ so its reverse will be ‘nohtyP’. This is what we’ll implement practically using multiple Python code examples.
Implementing Reverse String In Python (3 Examples)
There is no built-in function to reverse a Python string. So we’ll be using slicing method to reverse string in Python. See below examples for better understanding.
Example 1: Reverse a Simple String
name='Python' print(name) reversedString=name[::-1] print(reversedString)
Output
Python // simple string nohtyP // reversed string
- We’ve created a simple Python string and store it in a variable.
- After that, we’ve used string slicing on it.
- [starting:ending:step] In this syntax, starting specifies the starting index of string and ending specifies the ending index. In step, we can specify if we want to get all the characters of the string or not.
- [::-1] It specifies that the range will cover all the items of string. Negative index specifies that start the string from right/end.
- So in this way, we’ve got reverse string in Python.
See other examples as well.
print(name[::-2]) print(name[::-3]) print(name[2::-3]) print(name[1:5:-2])
Output
Python nohtyP nhy nt t
Example 2: Reverse a List of Strings in Python For Loop
listItems=['This','is','Python','Programming','Language'] reversedList=[] # reverse list of strings for var in listItems: reversedList.append(var[::-1]) # show reversed list of strings for var in reversedList: print(var)
Output
sihT si nohtyP gnimmargorP egaugnaL
- We’ve created a Python list which consists of string items.
- We’ve also created an empty Python list to store the reversed list of strings.
- After that, we’ve created a Python for loop which will take each item from the list and reverse it using string slicing method. Also, it’ll add the reversed strings to the new list using Python list append method.
- Second Python for loop is used to print every item of the new(reversed items) list.
Example 3: Reverse a List of Strings in Python While Loop
listItems=['This','is','Python','Programming','Language'] reversedList=[] i=0 while(i<len(listItems)): reversedList.append(listItems[i][::-1]) i=i+1 print(reversedList)
Output
['sihT', 'si', 'nohtyP', 'gnimmargorP', 'egaugnaL']
- We’ve created a list of strings. Also, we’ve created an empty list which will store the reversed list of strings.
- After that, we’ve created an integer variable whose value will be incremented by 1 in each loop and also with a condition that’ll check whether the length of list is greater than integer or not. If its greater, then continue the while loop, or else terminates it.
- Inside the loop, each item(string) will be reversed using string slicing method and added to the new list using append method.
- Finally, the whole Python list is shown using print function.
So this is how we can easily reverse string in Python. I’ve tried to explain it using multiple examples for better understanding. Hope you like this post.
Don’t hesitate to ask if you still have any questions regarding how to reverse string in Python. I’ll be very glad to answer all.
Conclusion
To conclude this tutorial, hope you now have a detailed practical understanding of how to properly reverse string in Python. I’ll be very happy to receive your feedback on this post.
Do visit my other articles on Python programming. Links to some of them are listed below. Others can be found using the navigation bar or search box of this site. Thank you for reading this post.
You may like to read:
How To Easily Get Input In Python [Python Code Examples]
[Solved] How To Find Maximum Value In Python Tuple
[Solved] How To Convert Python Tuple To String
How To Easily Use Python String Split Method [Detailed Python Guide]
How To Find Minimum Value In Python Tuple [Detailed Python Guide]