October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Reverse a Number in Python

Reverse an integer in Python with string slicing for the simplest solution, or use modulo and floor division when string conversion is off limits.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For an integer result, convert the number to text, reverse the digits with slicing, and convert back: int(str(n)[::-1]). Use an arithmetic loop instead if string conversion is not allowed, and return a string if leading zeroes must remain visible.

Reverse an integer with string slicing

For a non-negative integer, this is the shortest approach:

def reverse_number(n: int) -> int:
    return int(str(n)[::-1])

print(reverse_number(12345))  # 54321

str(n) creates the digit text, [::-1] reads it backward, and int(...) converts the result back to an integer. Python’s numeric type reference distinguishes integers from floats; this recipe is for integers, not decimal values.

Handle negative integers

Reverse the magnitude and restore the sign separately, so the minus sign is not treated as a digit:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Five Star Spiral Notebook, 1 Subject, College Ruled Paper, 4-3/8" x 7", Small Size, 80 Sheets, Fights Ink Bleed, Water Resistant Cover, Seaglass Green (450048CH1-ECM)
  • This 4-3/8" x 7" small size, 1 subject notebook has 80 double-sided college ruled sheets that fight ink bleed and are perforated for easy tear out. Perfectly sized for when you're on the go.
  • Tough pockets resist tears and hold loose sheets and notes. Durable plastic water-resistant front cover helps protect your notes and our Spiral Lock wire helps prevent snags on clothes and backpacks.
  • All the benefits of our larger notebooks in a smaller, easy to carry size. Sheets measure 4-3/8" x 7 when torn out.
  • Available in Seaglass Green
  • LASTS ALL YEAR. GUARANTEED!*
def reverse_number(n: int) -> int:
    sign = -1 if n < 0 else 1
    return sign * int(str(abs(n))[::-1])

Choose whether zeroes should be preserved

An integer cannot retain leading zeroes. Reversing 1200 and converting the result to an integer produces 21, not 0021. If the reversed digits are meant to be displayed with zeroes intact, return text instead:

def reverse_digits_text(n: int) -> str:
    sign = "-" if n < 0 else ""
    return sign + str(abs(n))[::-1]

print(reverse_digits_text(1200))  # '0021'

Reverse digits with arithmetic

If an exercise requires a loop or forbids converting the number to a string, extract one digit at a time with remainder and floor division:

Rank #2
Oxford Spiral Notebook 6 Pack, 1 Subject, College Ruled Paper, 8 x 10-1/2 Inch, Color Assortment Design May Vary (65007)
  • A classroom classic: this 6-pack of 1-subject spiral notebooks helps you identify your subjects at a glance with color-coding efficiency; color assortment may vary
  • The right ruling: these 8" x 10-1/2", college-ruled notebooks fit more writing per page than wide-ruled sheets; each notebook provides 70 double-sided sheets with red margin lines
  • Perect perforation: Dependable micro-perforated sheets retain your must-have notes but still detach cleanly when you’re ready to revise
  • Glide from page to page: Your favorite gel or ballpoint pens will move effortlessly across these smooth pages for A+ notes with minimal ink bleeding or show-through
  • 3-Hold punched: Every notebook comes 3-hole punched to fit a standard binder; take along one notebook or several to save extra trips to the locker
def reverse_number_arithmetic(n: int) -> int:
    sign = -1 if n < 0 else 1
    n = abs(n)
    reversed_number = 0

    while n:
        digit = n % 10
        reversed_number = reversed_number * 10 + digit
        n //= 10

    return sign * reversed_number

print(reverse_number_arithmetic(1234))  # 4321

Each loop iteration extracts the last digit with % 10, shifts the accumulated answer one decimal place by multiplying by 10, then removes the processed digit with // 10. The Python numeric-operator reference defines divmod(x, y) as (x // y, x % y) and notes that floor division rounds toward minus infinity. Here the input is made non-negative first, so these operations process ordinary decimal digits. For input 0, the loop is skipped and the initialized result remains 0.

Which method should you use?

Need Use Result for 1200
Shortest, clearest integer solution String slicing with int(...) 21
Arithmetic-only digit processing Remainder and floor-division loop 21
Preserve all reversed digits, including leading zeroes Return reversed text '0021'

Both integer-returning approaches reverse the digits of non-negative integers; the arithmetic version is useful when the method itself is part of the exercise. Define negative-number handling explicitly if your assignment specifies a different convention.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Five Star Spiral Notebook, 2 Subject, College Ruled Paper, 6" x 9.5", 80 Sheets, Blue (840029CG1)
  • Perfectly sized for when you're on the go, this small 2 subject notebook has 80 double-sided college ruled sheets that fight ink bleed and are perforated for easy tear out
  • Tough pockets help prevent tears and hold 6" x 9-1/2" loose sheets and notes. Durable plastic water-resistant front cover helps protect your notes and our Spiral Lock wire helps prevent snags on clothes and backpacks.
  • All the benefits of our larger notebooks in a smaller, easy to carry size. Sheets measure 6" x 9-1/2" when torn out.
  • Made with SFI certified paper. Notebook is recyclable – just remove the reinforcement tape on the pocket and recycle the rest! Available in Blue (Color May Vary)
  • LASTS ALL YEAR. GUARANTEED!*
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What about floating-point numbers?

Do not apply the integer recipe to a float without deciding what reversing a decimal means. The decimal point is not a digit, and Python’s numeric type reference states that converting a float to int truncates its fractional part. For example, converting 12.34 to int first discards the fraction; that is not the same as reversing the digits of the original decimal text. Specify whether the decimal point, sign, and zero padding should be preserved before implementing a float-specific rule.

Best Value
PAPERAGE Lined Journal Notebook, Hardcover Journal for Women & Men, 160 Pages, (5.6 in x 8 in), College Ruled Journaling Notebook for Work, School Supplies & Note Taking, (Black)
  • BEST-SELLING HARDCOVER JOURNAL: This classic 5.6" x 8" vegan leather journal features a durable and water-resistant cover, 160 college ruled lined pages, inner expandable pocket, sticker labels, ribbon bookmark & elastic closure band.
  • PREMIUM PAPER: Made with high-quality, 100 gsm acid-free paper in light ivory color, our journal paper is thicker than average notebooks & note pads, so you can confidently use most pens, pencils, and markers without ghosting and bleed-through.
  • LAY FLAT DESIGN FOR WRITING EASE: Our thread-bound, college ruled notebook is designed to lay flat, making it easier to write for both right and left-handed users. It’s the perfect notebook for journaling, note taking and planning.
  • INNER POCKET: Includes an expandable inner storage pocket to store appointment cards, notes, receipts, and more. Personalize your journal cover & spine with the sheet of sticker labels included.
  • VERSATILE LINED NOTEBOOK: Ideal for journaling, note-taking, planning, or creative writing. Whether you're making a to-do list, capturing ideas, or writing notes, this journal makes a perfect notebook for school, work, or home office.
Rank #4
Sale
Five Star Spiral Notebook + Study App, 5 Subject, College Ruled Paper, 8-1/2" x 11", 200 Sheets, Fights Ink Bleed, Water Resistant Cover, Pacific Blue (73635)
  • LASTS ALL YEAR. GUARANTEED! Guarantee is valid for one year from purchase or delivery date, whichever is longer. Does not cover misuse.
  • Scan, study and organize your notes with the Five Star Study App. Create instant flashcards and sync your notes to Google Drive to access them anywhere from any device.
  • This 5 subject notebook has 200 double-sided, college ruled sheets that fight ink bleed and are perforated for easy tear out. Sheets measure 8-1/2" x 11" when torn out.
  • Tough pockets help prevent tears and hold 8-1/2" x 11" loose sheets. Durable plastic front cover is water-resistant to help protect your notes and our Spiral Lock wire helps prevent snags on clothes and backpacks.
  • Made with SFI certified paper. Notebook is recyclable – just remove the reinforcement tape on the pocket and recycle the rest! Available in Pacific Blue.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.