Python Program to Reverse a String
You can reverse a string in Python using slicing like this:
reversed_string = original_string[::-1] which creates a new string with characters in reverse order.Examples
Inputhello
Outputolleh
InputPython
OutputnohtyP
Input
Output
How to Think About It
To reverse a string, think about reading the characters from the end to the start. Instead of going from left to right, you go from right to left, collecting each character until you reach the beginning.
Algorithm
1
Get the input string from the user or a variable.2
Start from the last character of the string.3
Collect characters moving backward until the first character.4
Combine these characters to form the reversed string.5
Return or print the reversed string.Code
python
original_string = input("Enter a string: ") reversed_string = original_string[::-1] print("Reversed string:", reversed_string)
Output
Enter a string: hello
Reversed string: olleh
Dry Run
Let's trace the string 'hello' through the code
1
Input string
original_string = 'hello'
2
Reverse using slicing
reversed_string = original_string[::-1] results in 'olleh'
3
Print result
Output: Reversed string: olleh
| Index | Character |
|---|---|
| 4 | o |
| 3 | l |
| 2 | l |
| 1 | e |
| 0 | h |
Why This Works
Step 1: Slicing with negative step
Using [::-1] tells Python to take the string from end to start stepping backwards by 1, effectively reversing it.
Step 2: Creating a new string
This slicing creates a new string with characters in reverse order without changing the original string.
Alternative Approaches
Using a loop
python
original_string = input("Enter a string: ") reversed_string = '' for char in original_string: reversed_string = char + reversed_string print("Reversed string:", reversed_string)
This method builds the reversed string by adding each character to the front, but is less concise than slicing.
Using reversed() and join()
python
original_string = input("Enter a string: ") reversed_string = ''.join(reversed(original_string)) print("Reversed string:", reversed_string)
This uses Python's built-in reversed() function and joins characters back into a string, which is clear and readable.
Complexity: O(n) time, O(n) space
Time Complexity
Reversing a string requires visiting each character once, so it takes linear time proportional to the string length.
Space Complexity
A new string is created to hold the reversed characters, so space used is also proportional to the string length.
Which Approach is Fastest?
Slicing [::-1] is the fastest and most concise method, while loops and reversed() are slightly slower but more explicit.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Slicing [::-1] | O(n) | O(n) | Quick and concise reversal |
| Loop building string | O(n) | O(n) | Understanding string immutability |
| reversed() + join() | O(n) | O(n) | Readable and Pythonic |
Use slicing
[::-1] for the simplest and fastest way to reverse a string in Python.Beginners often try to reverse strings by modifying them in place, but strings in Python are immutable and cannot be changed directly.