๐ Day 62/150 – Reverse a String in Python
Reversing a string means arranging its characters in the opposite order.
Example:
"python" → "nohtyp"
Let’s explore different ways to reverse a string ๐
๐น Method 1 – Using Slicing
text = "python"
reversed_text = text[::-1]
print("Reversed String:", reversed_text)
✅ Shortest and most common method.
๐น Method 2 – Using for Loop
text = "python" reversed_text = "" for ch in text: reversed_text = ch + reversed_text print("Reversed String:", reversed_text)
✅ Good for understanding the logic.
๐น Method 3 – Using reversed()
text = "python" reversed_text = ''.join(reversed(text)) print("Reversed String:", reversed_text)
✅ Uses Python’s built-in iterator.
๐น Method 4 – Taking User Input
text = input("Enter a string: ") print("Reversed String:", text[::-1])
✅ Dynamic version.
๐น Method 5 – Using Recursion
def reverse_string(s):
if s == "":
return s
return reverse_string(s[1:]) + s[0]
print(reverse_string("python"))
✅ Useful for learning recursion.
๐ก Key Takeaways
- Slicing [::-1] is the easiest way
- Strings are immutable, so a new string is created
- Loop and recursion help understand how reversing works
- Commonly used in palindrome and string-processing problems

