- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to reverse a string in Python?
Reversing a string in Python means creating a new string that is a reversed copy of the original string.
Here are some different ways to reverse a string in Python −
Using slicing
In this example, we use slicing to create a new string that is a reversed copy of the original string. The syntax [start:stop:step] means that we start at the end of the string and step backwards by 1 until we reach the beginning.
Example
original_string = "lorem ipsum" reversed_string = original_string[::-1] print(reversed_string)
Output
muspi merol
Using a for loop
In this example, we use a ‘for loop’ to iterate over the characters of the original string in reverse order. We start at the last character (index len(original_string)-1) and move backwards by 1 until we reach the first character (index 0). We then add each character to a new string called reversed_string.
Example
original_string = "lorem ipsum" reversed_string = "" for i in range(len(original_string)-1, -1, -1): reversed_string += original_string[i] print(reversed_string)
Output
muspi merol
Using the reversed() function
In this example, we use the built-in reversed() function to create a reverse iterator over the characters of the original string. We then use a ‘for loop’ to iterate over the iterator and add each character to a new string called reversed_string.
Example
original_string = "lorem ipsum" reversed_string = "" for char in reversed(original_string): reversed_string += char print(reversed_string)
Output
muspi merol
Using recursion
In this example, we define a recursive function called reverse_string that takes a string as input and returns a reversed copy of the string. The function works by calling itself recursively with the first character removed from the input string (s[1:]) and then adding the first character to the end of the reversed substring (s[0]). The recursion stops when the length of the input string reaches zero.
Example
def reverse_string(s): if len(s) == 0: return s else: return reverse_string(s[1:]) + s[0] original_string = "lorem ipsum" reversed_string = reverse_string(original_string) print(reversed_string)
Output
muspi merol
Using a while loop
In this example, we use a while loop to iterate over the characters of the original string in reverse order. We start at the last character by setting the index to len(original_string) - 1, and we continue looping as long as the index is greater than or equal to zero. Inside the loop, we add each character to a new string called reversed_string, and we decrement the index by one on each iteration.
Example
original_string = "lorem ipsum" reversed_string = "" # Start with the last character of the original string index = len(original_string) - 1 # Loop backwards over the string and add each character to the new string while index >= 0: reversed_string += original_string[index] index -= 1 print(reversed_string)
Output
muspi merol
Using a list and the join() method
In this example, we first convert the original string to a list of characters using the list() function. We can then use the reverse() method of the list to reverse the order of the characters in place. Finally, we use the join() method of an empty string to concatenate the characters back into a new string called reversed_string.
Example
original_string = "lorem ipsum" character_list = list(original_string) # Reverse the list using the reverse() method character_list.reverse() # Convert the list back to a string using the join() method reversed_string = "".join(character_list) print(reversed_string)
Output
muspi merol