{"id":76,"date":"2025-05-12T12:23:23","date_gmt":"2025-05-12T06:53:23","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=76"},"modified":"2025-07-07T19:51:31","modified_gmt":"2025-07-07T14:21:31","slug":"extraction-of-digits-in-python","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/extraction-of-digits-in-python\/","title":{"rendered":"Extraction of Digits in Python | Explained Step-by-Step"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>By the end of this article, you&#8217;ll have a fair idea on how to extract digits in Python.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-ef9e7009-4a4c-4604-9d6f-b3ce1e4aacd2\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Content:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/extraction-of-digits-in-python\/#0-1what-does-the-problem-extraction-of-digits-in-python-say\" style=\"\">1.What does the Problem (Extraction of Digits in Python) say?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/extraction-of-digits-in-python\/#1-2intuition-behind-the-solution\" style=\"\">2.Intuition behind the Solution<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/extraction-of-digits-in-python\/#2-3code-extract-digits-in-python\" style=\"\">3.Code (extract digits in Python)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/extraction-of-digits-in-python\/#3-4explanation\" style=\"\">4.Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/extraction-of-digits-in-python\/#4-5dry-run\" style=\"\">5.Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/extraction-of-digits-in-python\/#5-6edge-cases\" style=\"\">6.Edge Cases<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-1what-does-the-problem-extraction-of-digits-in-python-say\">1.What does the Problem (Extraction of Digits in Python) say?<\/h2>\n\n\n\n<p>The problem is to extract &amp; print each digit of a given integer number starting from the last digit. The digits should be printed without spaces or any separators.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-vivid-purple-color has-text-color has-link-color wp-elements-1addac36f941b0680973248f953377ef\"><em>For example, given the input 1234, the output should be 4321.<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-2intuition-behind-the-solution\">2.Intuition behind the Solution<\/h2>\n\n\n\n<p>To solve this problem, the intuitive approach is to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Isolate the last digit of the number by using the modulus operation (% 10)<\/li>\n\n\n\n<li>Print the isolated digit<\/li>\n\n\n\n<li>Remove the last digit from the number by using integer division (\/\/ 10)<\/li>\n\n\n\n<li>Repeat the process until all digits have been extracted and printed<\/li>\n<\/ul>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Repeating these steps will eventually extract and print all digits in reverse order.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-3code-extract-digits-in-python\">3.Code (extract digits in Python)<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def extractDigits(num: int) -&gt; None:\n    n = num\n    while n &gt; 0:\n        last_digit = n % 10\n        print(last_digit, end=&quot;&quot;)\n        n = n \/\/ 10\n\n\nextractDigits(4356)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">extractDigits<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">num<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    n = num<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> n &gt; <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        last_digit = n % <\/span><span style=\"color: #B5CEA8\">10<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(last_digit, <\/span><span style=\"color: #9CDCFE\">end<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&quot;&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = n \/\/ <\/span><span style=\"color: #B5CEA8\">10<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">extractDigits(<\/span><span style=\"color: #B5CEA8\">4356<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-4explanation\">4.Explanation<\/h2>\n\n\n\n<p>The provided code, <strong>extractDigits(num: int) -&gt; None<\/strong>, does the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Takes an integer &#8220;num&#8221; as input<\/li>\n\n\n\n<li>Uses a &#8220;while loop&#8221; to repeatedly extract the last digit of the number &amp; print it until the number becomes zero<\/li>\n\n\n\n<li>Inside the loop:\n<ul class=\"wp-block-list\">\n<li>The last digit is obtained using the modulus operation (n % 10)<\/li>\n\n\n\n<li>The last digit is printed<\/li>\n\n\n\n<li>The number is updated to exclude the last digit by using integer division (n \/\/ 10)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-5dry-run\">5.Dry Run<\/h2>\n\n\n\n<p>Let&#8217;s dry run the code with an example below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/extraction-of-digits-1024x576.png\" alt=\"Extraction of Digits - Dry Run - Graphical Representation\" class=\"wp-image-77\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/extraction-of-digits-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/extraction-of-digits-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/extraction-of-digits-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/extraction-of-digits-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/extraction-of-digits-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/extraction-of-digits-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/extraction-of-digits-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/extraction-of-digits.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-6edge-cases\">6.Edge Cases<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Single digit input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If &#8220;num&#8221; is a single digit, say 5, the output will be 5, which is the same as the input.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Zero input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If &#8220;num&#8221; 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.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Negative input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The provided code does not handle negative numbers. If &#8220;num&#8221; is -1234, the code will not work as intended. To handle negative numbers, you could take the absolute value of &#8220;num&#8221; before processing.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-white-color has-vivid-purple-background-color has-text-color has-background has-link-color has-text-align-center wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Master DSA with our course<\/a><\/div>\n<\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>For any changes to the document, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n<\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;ll have a fair idea on how to extract digits in Python. 1.What does the Problem (Extraction of Digits in Python) say? The problem is to<\/p>\n","protected":false},"author":1,"featured_media":78,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[8,10],"class_list":{"0":"post-76","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-easy","10":"tag-math"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Extraction-of-Digits-in-Python.webp","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":3,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":539,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions\/539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/78"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}