{"id":4307,"date":"2026-11-23T12:02:37","date_gmt":"2026-11-23T12:02:37","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=4307"},"modified":"2026-11-23T12:02:44","modified_gmt":"2026-11-23T12:02:44","slug":"python-program-to-find-first-and-last-digit-in-a-number","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-program-to-find-first-and-last-digit-in-a-number\/","title":{"rendered":"Python Program to Find the First and Last Digit of a Number (3 Methods)"},"content":{"rendered":"<p>Given a number such as <code>34759<\/code>, the task is to find its first digit (<code>3<\/code>) and last digit (<code>9<\/code>). This is a common beginner exercise in Python, and it can be solved in three ways: by converting the number to a string, with a loop, or with logarithms. This page shows all three, explains which one to use, and covers negative numbers and other tricky inputs.<\/p>\n<h2>Finding the Last Digit<\/h2>\n<p>The last digit of a number is the remainder when you divide it by 10:<\/p>\n<pre class=\"wp-block-code\"><code>number = 34759\nlast_digit = number % 10\nprint(last_digit)   # 9<\/code><\/pre>\n<h2>Method 1: Using a String (Simplest)<\/h2>\n<p>Convert the number to a string, then take the first character (<code>s[0]<\/code>) and the last character (<code>s[-1]<\/code>):<\/p>\n<pre class=\"wp-block-code\"><code>number = int(input(\"Enter a number: \"))\n\ns = str(abs(number))      # abs() removes a minus sign, if any\nfirst_digit = int(s[0])\nlast_digit = int(s[-1])\n\nprint(\"First digit:\", first_digit)\nprint(\"Last digit:\", last_digit)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Enter a number: 34759\nFirst digit: 3\nLast digit: 9<\/code><\/pre>\n<h2>Method 2: Using a Loop (Pure Maths)<\/h2>\n<p>Keep dividing the number by 10 with integer division (<code>\/\/<\/code>) until only one digit is left. That digit is the first digit.<\/p>\n<pre class=\"wp-block-code\"><code>number = int(input(\"Enter a number: \"))\nn = abs(number)\n\nlast_digit = n % 10\n\nfirst_digit = n\nwhile first_digit &gt;= 10:\n    first_digit \/\/= 10\n\nprint(\"First digit:\", first_digit)\nprint(\"Last digit:\", last_digit)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Enter a number: 47982\nFirst digit: 4\nLast digit: 2<\/code><\/pre>\n<p>For 47982, the loop runs 47982 \u2192 4798 \u2192 479 \u2192 47 \u2192 4, then stops because 4 is less than 10.<\/p>\n<h2>Method 3: Using Logarithms (and Its Hidden Problem)<\/h2>\n<p>The number of digits in <em>n<\/em> is <code>floor(log10(n)) + 1<\/code>, so dividing <em>n<\/em> by 10 raised to <code>floor(log10(n))<\/code> gives the first digit:<\/p>\n<pre class=\"wp-block-code\"><code>import math\n\nnumber = 34759\nfirst_digit = number \/\/ 10 ** math.floor(math.log10(number))\nprint(first_digit)   # 3<\/code><\/pre>\n<p>This works for ordinary numbers, but it has two problems:<\/p>\n<ul>\n<li>It crashes for <code>0<\/code> and negative numbers with <code>ValueError: math domain error<\/code>.<\/li>\n<li>For very large numbers, floating-point rounding can give a wrong answer. For example, <code>math.log10(999999999999999999)<\/code> returns <code>18.0<\/code> instead of a value just below 18, so the formula gives <code>0<\/code> instead of <code>9<\/code>.<\/li>\n<\/ul>\n<p>Because of this, prefer Method 1 or Method 2 in real programs.<\/p>\n<h2>Watch Out for Negative Numbers<\/h2>\n<p>In Python, <code>%<\/code> with a positive divisor always returns a non-negative result, so the last-digit trick gives a surprising answer for negative numbers:<\/p>\n<pre class=\"wp-block-code\"><code>print(-5821 % 10)        # 9, not 1\nprint(abs(-5821) % 10)   # 1<\/code><\/pre>\n<p>That is why both programs above use <code>abs()<\/code> first.<\/p>\n<h2>Test Your Program with These Inputs<\/h2>\n<table>\n<thead>\n<tr>\n<th>Input<\/th>\n<th>First digit<\/th>\n<th>Last digit<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>34759<\/td>\n<td>3<\/td>\n<td>9<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>7<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td>1000<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>-5821<\/td>\n<td>5<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>999999999999999999<\/td>\n<td>9<\/td>\n<td>9<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Methods 1 and 2 give the correct answer for every row in this table.<\/p>\n<h2>Practice Variation: Sum of the First and Last Digit<\/h2>\n<pre class=\"wp-block-code\"><code>number = 34759\ns = str(abs(number))\nprint(int(s[0]) + int(s[-1]))   # 3 + 9 = 12<\/code><\/pre>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Python Program to Find First and Last Digit in a Number\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/bqfjH8WHtFg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div>\n<\/figure>\n<h2>Frequently Asked Questions<\/h2>\n<h3>How do I get the last digit of a number in Python?<\/h3>\n<p>Use <code>abs(number) % 10<\/code>.<\/p>\n<h3>How do I get the first digit of a number in Python?<\/h3>\n<p>The simplest way is <code>int(str(abs(number))[0])<\/code>. Without strings, divide by 10 in a loop until the number is less than 10.<\/p>\n<h3>Which method is fastest?<\/h3>\n<p>For normal-sized numbers, all three are instant. The string method is the easiest to read, and the loop method is what teachers usually expect when they say &#8220;without using strings&#8221;.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/python-syntax\/\">Writing your first Python program<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a number such as 34759, the task is to find its first digit (3) and last digit (9). This is a common beginner exercise in Python, and it can be solved in three ways: by converting the number to a string, with a loop, or with logarithms. This page shows all three, explains which &#8230; <a title=\"Python Program to Find the First and Last Digit of a Number (3 Methods)\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-program-to-find-first-and-last-digit-in-a-number\/\" aria-label=\"Read more about Python Program to Find the First and Last Digit of a Number (3 Methods)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[26,42],"class_list":["post-4307","post","type-post","status-publish","format-standard","hentry","category-python","tag-python","tag-python-program-to-find-first-and-last-digit-in-a-number"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=4307"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4307\/revisions"}],"predecessor-version":[{"id":6155,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4307\/revisions\/6155"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=4307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=4307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=4307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}