How to Reverse a String, List, or Number in Python

Updated On: 15 March 2026

Reversing a string, list, or number in Python is one of those small problems that shows up everywhere.

You will see it in beginner coding practice, Python interviews, school assignments, and sometimes even inside real projects where data needs to be rearranged or cleaned before processing.

The good part is that Python gives you multiple ways to do it.

You can reverse values using a normal loop, a slicing trick, or built-in Python functions. All of them work, but each one makes sense in a slightly different situation.

In this post, we will go through three simple ways to reverse a string, list, and number in Python, with examples that are easy to understand even if you are just starting out.


Method 1: Reverse Using Loops

The first way is the traditional way. We use a loop and build the reversed output step by step.

This method is great for understanding the logic behind reversing, even if it is not always the shortest solution.

Reverse a String Using for Loop

def reverse_string(text):
    reversed_text = ""
    for ch in text:
        reversed_text = ch + reversed_text
    return reversed_text

print(reverse_string("DSFOR"))

Output

ROFSD

Here, each character is added at the front of the new string, so the final string becomes reversed.


Reverse a List Using while Loop

def reverse_list(items):
    index = len(items) - 1
    reversed_items = []    while index >= 0:
        reversed_items.append(items[index])
        index -= 1    return reversed_items

print(reverse_list(["python", "is", "cool"]))

Output

['cool', 'is', 'python']

This works by starting from the last element and moving backward until the list is fully reversed.


Reverse a Number Using while Loop

def reverse_number(number):
    reversed_num = 0    while number > 0:
        digit = number % 10
        reversed_num = reversed_num * 10 + digit
        number = number // 10    return reversed_num

print(reverse_number(12345))

Output

54321

This is a good interview-style solution because it reverses the number mathematically without converting it to a string.


Method 2: Reverse Using Slicing

If you have used Python for even a little while, you have probably seen this already.

Slicing is one of the easiest and most Pythonic ways to reverse a sequence.

The [::-1] syntax means:

  • start from the end
  • move backward
  • take every element

For strings and lists, this is often the cleanest method.


Reverse a String Using Slicing

def reverse_string(text):
    return text[::-1]print(reverse_string("DSFOR"))

Output

ROFSD

Reverse a List Using Slicing

def reverse_list(items):
    return items[::-1]print(reverse_list(["python", "is", "cool"]))

Output

['cool', 'is', 'python']

Reverse a Number Using Slicing

Since numbers are not sequences, we first convert the number to a string.

def reverse_number(number):
    return str(number)[::-1]print(reverse_number(12345))

Output

54321

One small thing to notice here: this returns a string, not an integer.

If you want the result back as an integer, do this:

def reverse_number(number):
return int(str(number)[::-1])print(reverse_number(12345))

Method 3: Reverse Using Built-in Python Functions

Python also gives you built-in ways to reverse values without writing the full logic manually.

This is often the most readable approach once you are comfortable with the language.


Reverse a String Using reversed()

def reverse_string(text):
    return "".join(reversed(text))print(reverse_string("DSFOR"))

Output

ROFSD

The reversed() function returns an iterator, so for strings we join the characters back together using "".join().


Reverse a List Using .reverse()

def reverse_list(items):
    items.reverse()
    return itemsprint(reverse_list([11, 4, 12, 8, 3, 6]))

Output

[6, 3, 8, 12, 4, 11]

This works fine, but there is one thing beginners often miss:

.reverse() changes the original list in place.

That means the original list itself gets modified.

If you do not want to modify the original list, use slicing or list(reversed(items)) instead.


Reverse a Number Using reversed()

def reverse_number(number):
    return "".join(reversed(str(number)))print(reverse_number(123456))

Output

654321

Again, this returns a string. If needed, wrap it with int().


Quick Comparison of All 3 Methods

MethodBest ForEasy for BeginnersNotes
LoopLearning logic and interviewsYesGood for understanding how reverse works internally
SlicingStrings and listsYesShort, clean, and widely used in Python
Built-in functionsReadable Python codeYesUseful once you know how reversed() and .reverse() behave

Which Method Should You Use?

If you are preparing for interviews, it is worth knowing all three.

If you just want the quickest and cleanest answer in Python:

  • use slicing for strings and lists
  • use math logic or string conversion for numbers depending on the situation

In real projects, readability matters more than showing off clever tricks. So choose the method that is easiest to understand when someone else reads your code later.


Important Difference Between reversed() and .reverse()

This is something that often confuses beginners.

reversed()

  • returns an iterator
  • does not modify the original sequence directly
  • works well with strings, tuples, and lists

.reverse()

  • is a list method
  • modifies the original list in place
  • works only on lists

Example:

nums = [1, 2, 3, 4]
print(list(reversed(nums)))  # [4, 3, 2, 1]
print(nums)                  # [1, 2, 3, 4]

nums.reverse()
print(nums)                  # [4, 3, 2, 1]

That difference is small, but very important.


Reversing a string, list, or number in Python is a basic problem, but it teaches something useful: Python often gives you more than one way to solve the same task.

You can use loops to understand the core logic.
You can use slicing for a neat Python shortcut.
And you can use built-in functions when you want readable code with less effort.

For most day-to-day Python work, slicing is usually the simplest choice for strings and lists. But if someone asks you this in an interview, knowing the loop-based approach too will make you look much more confident.

Leave a Reply

Scroll to Top