Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»Data Structures & Algorithms»Extraction of Digits in Python | Explained Step-by-Step
    Data Structures & Algorithms

    Extraction of Digits in Python | Explained Step-by-Step

    codeanddebugBy codeanddebug12 May 2025Updated:12 May 2025No Comments3 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Welcome to this article on extraction of digits in Python. Explained step-by-step with the intuitive approach, code, explanation, dry run and edge cases.

    By the end of this article, you’ll have a fair idea on how to extract digits in Python.

    Content:
     [show]
    • 1.What does the Problem (Extraction of Digits in Python) say?
    • 2.Intuition behind the Solution
    • 3.Code (extract digits in Python)
    • 4.Explanation
    • 5.Dry Run
    • 6.Edge Cases

    1.What does the Problem (Extraction of Digits in Python) say?

    The problem is to extract & print each digit of a given integer number starting from the last digit. The digits should be printed without spaces or any separators.

    For example, given the input 1234, the output should be 4321.

    2.Intuition behind the Solution

    To solve this problem, the intuitive approach is to:

    • Isolate the last digit of the number by using the modulus operation (% 10)
    • Print the isolated digit
    • Remove the last digit from the number by using integer division (// 10)
    • Repeat the process until all digits have been extracted and printed

    This works because the modulus operation gives the remainder when the number is divided by 10, which is effectively the last digit of the number. Integer division by 10 removes the last digit from the number.

    Repeating these steps will eventually extract and print all digits in reverse order.

    3.Code (extract digits in Python)

    def extractDigits(num: int) -> None:
        n = num
        while n > 0:
            last_digit = n % 10
            print(last_digit, end="")
            n = n // 10
    
    extractDigits(4356)

    4.Explanation

    The provided code, extractDigits(num: int) -> None, does the following:

    • Takes an integer “num” as input
    • Uses a “while loop” to repeatedly extract the last digit of the number & print it until the number becomes zero
    • Inside the loop:
      • The last digit is obtained using the modulus operation (n % 10)
      • The last digit is printed
      • The number is updated to exclude the last digit by using integer division (n // 10)

    5.Dry Run

    Let’s dry run the code with an example below:

    Extraction of Digits - Dry Run - Graphical Representation

    6.Edge Cases

    1. Single digit input:
      • If “num” is a single digit, say 5, the output will be 5, which is the same as the input.
    2. Zero input:
      • If “num” is 0, the code will not enter the while loop and will not print anything. Depending on the requirement, you might want to handle this case separately to ensure 0 is printed.
    3. Negative input:
      • The provided code does not handle negative numbers. If “num” is -1234, the code will not work as intended. To handle negative numbers, you could take the absolute value of “num” before processing.
    Master DSA with our course

    For any changes to the document, kindly email at code@codeanddebug.in or contact us at +91-9712928220.

    Easy Math
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleTime Complexity and Space Complexity in DSA | Explained with Examples
    Next Article Python Program to Count Number of Digits | Explained
    codeanddebug
    • Website

    Related Posts

    Data Structures & Algorithms

    Diameter of Binary Tree

    14 May 2025
    Data Structures & Algorithms

    Maximum Depth of Binary Tree | Explained with Examples

    14 May 2025
    Data Structures & Algorithms

    Breadth-First Search in Binary Trees: Level-Order Traversal with Python

    12 May 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • Data Structures & Algorithms (13)
      • Beginner (9)
      • Intermediate (4)
    Recent Posts

    Diameter of Binary Tree

    14 May 2025

    Maximum Depth of Binary Tree | Explained with Examples

    14 May 2025

    Breadth-First Search in Binary Trees: Level-Order Traversal with Python

    12 May 2025

    A Beginner’s Guide to Preorder, Inorder & Postorder Traversals

    12 May 2025

    Python Program to Count Number of Digits | Explained

    12 May 2025
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.