I ran into this exact expression while solving a palindrome problem on a coding challenge platform. The line int(a[::-1]) looked cryptic at first glance — three colons, a negative step, wrapped in int(). But once I broke it down, it turned out to be one of the cleanest one-liners in Python for reversing and converting in a single shot.

This article covers how slice notation [::-1] reverses any sequence, how int() converts strings to integers, why combining them works, and where this idiom shows up in real code. By the end you’ll understand exactly what every character in that expression does.

TLDR

  • a[::-1] reverses any Python sequence using slice notation with step -1
  • int(a[::-1]) reverses a string and converts it to an integer in one expression
  • Leading zeros are dropped — int("00420"[::-1]) gives 24, not 4200
  • For integer input, convert first: int(str(n)[::-1])

What is int(a[::-1]) in Python?

int(a[::-1]) reverses a string and converts it to an integer. The slice [::-1] reverses the string, and int() parses the reversed string as a base-10 integer. For a = "12345", int(a[::-1]) produces 54321.

How Slice Notation [::-1] Works

The general form of slice notation is sequence[start:stop:step]. When step is -1, Python starts at the end and moves backwards one element at a time. Omitting start and stop means “from beginning to end,” so [::-1] literally means “from start to end, stepping by -1” — giving you the entire sequence reversed.

If the three-argument form is unfamiliar, the Python slice function article covers each parameter in detail with examples on strings, lists, and tuples.


word = "hello"
print(word[::-1])

numbers = [1, 2, 3, 4, 5]
print(numbers[::-1])

The slice returns a new reversed object — the original remains unchanged since strings and tuples are immutable. Lists are mutable but slicing still returns a new list. For more on working with strings this way, the extract digits from string article covers related string-to-number patterns.

The int() Function and String Conversion

Python’s int() function parses a string as a base-10 integer. It strips leading and trailing whitespace automatically, but leading zeros are dropped in the output — int("00123") returns 123. If the string contains invalid characters for the base, int() raises a ValueError.

The Python operators article covers the modulo and floor division operators used in the mathematical reversal approach below.


a = "12345"
reversed_str = a[::-1]
print(reversed_str)
print(int(reversed_str))
print(type(int(reversed_str)))

The expression int(a[::-1]) combines these two operations. First a[::-1] produces the reversed string, then int() converts it to an integer.

Reversing an Integer Directly

Integers don’t support slice notation — 12345[::-1] raises a TypeError. If your input is an integer, you must convert to string first: int(str(n)[::-1]). The reason is that integers are immutable in Python, much like tuples and frozensets — once created they cannot be modified.


n = 12345
print(int(str(n)[::-1]))

For a mathematical approach without string conversion, extract digits using modulo and division. This preserves leading zeros as trailing zeros in the output.


def reverse_number_math(n):
    result = 0
    while n > 0:
        result = result * 10 + n % 10
        n //= 10
    return result

print(reverse_number_math(12345))

The string-based approach is shorter and more readable. The mathematical approach avoids creating string objects and gives exact control over the reversal process.

Common Pitfalls

The most common mistake is passing an integer directly to [::-1]. Integers don’t support slice notation, so always convert to string first with int(str(n)[::-1]).


n = 12345
print(int(str(n)[::-1]))

The string conversion is required because Python sequences support slicing, but numeric types do not.

Leading zeros drop silently. Reversing "00420" gives "0240", and int("0240") returns 240 — not 4200. The reversed leading zeros become trailing zeros, which int() strips.


print(int("00420"[::-1]))

The output is 240 — the two leading zeros from the original become trailing zeros after reversal, then get dropped by int().

Floating-point numbers raise ValueError. The string "3.14"[::-1] produces "41.3", which int() cannot parse. For handling invalid input gracefully, the ValueError handling guide covers try/except patterns with integer conversion.

ValueError raised when int() encounters non-digit characters after slice reversal

try:
    print(int("3.14"[::-1]))
except ValueError as e:
    print(f"ValueError: {e}")

The except block catches the ValueError that occurs when int() tries to parse a string containing a decimal point.


ValueError: invalid literal for int() with base 10: '41.3'

FAQ

Q: What does [::-1] mean in Python?

The slice [::-1] reverses any sequence. It is equivalent to [start:stop:step] with start and stop omitted and step set to -1, meaning from the end to the start.

Q: Can int(a[::-1]) be used on a list?

No. [::-1] works on lists and returns a reversed list, but int() cannot convert a list to an integer. Use [::-1] alone for reversing lists.

Q: Does int(a[::-1]) preserve leading zeros?

No. int() drops leading zeros. Reversing "00100" gives "00100" (unchanged since it is a palindrome), and int("00100") returns 100.

Q: What is the reverse-and-add algorithm?

Reverse-and-add repeatedly reverses a number and adds it to itself until the result is a palindrome. For 195, it takes 4 steps to reach the palindrome 9339. Some numbers like 196 are suspected never to produce a palindrome through this process.

Share.
Leave A Reply