Given an integer greater than 9 print its last two digits python

With my code, I want to get the last two digits of an integer. But when I make x a positive number, it will take the first x digits, if it is a negative number, it will remove the first x digits.

Code:

number_of_numbers = 1
num = 9
while number_of_numbers <= 100:
  done = False
  num = num*10
  num = num+1
  while done == False:
    num_last = int(repr(num)[x])
    if num_last%14 == 0:
      number_of_numbers = number_of_numbers + 1
      done = True
    else:
      num = num + 1
print(num)

asked Jan 15, 2017 at 18:37

0

Why don't you extract the absolute value of the number modulus 100? That is, use

 abs(num) % 100 

to extract the last two digits?

In terms of performance and clarity, this method is hard to beat.

answered Jan 15, 2017 at 18:42

Given an integer greater than 9 print its last two digits python

BathshebaBathsheba

229k33 gold badges352 silver badges472 bronze badges

0

To get the last 2 digits of num I would use a 1 line simple hack:

str(num)[-2:]

This would give a string. To get an int, just wrap with int:

int(str(num)[-2:])

answered Jan 15, 2017 at 19:13

Given an integer greater than 9 print its last two digits python

Israel UntermanIsrael Unterman

12.6k4 gold badges27 silver badges34 bronze badges

Simpler way to extract last two digits of the number (less efficient) is to convert the number to str and slice the last two digits of the number. For example:

# sample function
def get_last_digits(num, last_digits_count=2):
    return int(str(num)[-last_digits_count:])
    #       ^ convert the number back to `int`

OR, you may achieve it via using modulo % operator (more efficient), (to know more, check How does % work in Python?) as:

def get_last_digits(num, last_digits_count=2):
    return abs(num) % (10**last_digits_count)
    #       ^ perform `%` on absolute value to cover `-`ive numbers

Sample run:

>>> get_last_digits(95432)
32
>>> get_last_digits(2)
2
>>> get_last_digits(34644, last_digits_count=4)
4644

answered Jan 15, 2017 at 18:40

Given an integer greater than 9 print its last two digits python

Moinuddin QuadriMoinuddin Quadri

44.7k12 gold badges92 silver badges118 bronze badges

2

to get the last 2 digits of an integer.

a = int(input())
print(a % 100)

answered Oct 11, 2019 at 7:36

You can try this:

float(str(num)[-2:])

answered Jan 7, 2021 at 22:38

  • Python code to extract and display the last two digits of a number.
    • Python code implementation without user defined functions & classes
    • Python code implementation using function
    • Python code implementation using Classes

 &lt;/p&gt; &lt;h3&gt;&lt;span class="ez-toc-section" id="Python_code_implementation_without_user_defined_functions_classes"&gt;&lt;/span&gt;&lt;strong&gt;&lt;span style="color:#ff0000"&gt;Python code implementation without user defined functions &amp;amp; classes&lt;/span&gt;&lt;/strong&gt;&lt;span class="ez-toc-section-end"&gt;&lt;/span&gt;&lt;/h3&gt; &lt;h5&gt;&lt;span style="color:#339966"&gt;&lt;strong&gt;Code:&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;/h5&gt; &lt;pre class="EnlighterJSRAW" data-enlighter-theme="atomic"&gt;# Python code to extract last two digits of a number a = 23457 a_string = str(a) a_length = len(a_string) c = int(a_string[a_length - 2: a_length]) print("The last two digit for the number: ", a, " is: ", c) &lt;/pre&gt; &lt;p&gt;&lt;span id="ezoic-pub-ad-placeholder-125" class="ezoic-adpicker-ad"&gt;&lt;/span&gt;&lt;span class="ezoic-ad ezoic-at-0 medrectangle-3 medrectangle-3125 adtester-container adtester-container-125 ezoic-ad-adaptive" data-ez-name="pythonbaba_com-medrectangle-3"&gt;&lt;span class="ezoic-ad medrectangle-3 medrectangle-3-multi-125 adtester-container adtester-container-125" data-ez-name="pythonbaba_com-medrectangle-3"&gt;&lt;span id="div-gpt-ad-pythonbaba_com-medrectangle-3-0" ezaw="290" ezah="250" style="position:relative;z-index:0;display:inline-block;padding:0;min-height:250px;min-width:290px" class="ezoic-ad"&gt;&lt;script data-ezscrex="false" data-cfasync="false" type="text/javascript" style="display:none"&gt;if(typeof ez_ad_units != 'undefined'){ez_ad_units.push([[250,250],'pythonbaba_com-medrectangle-3','ezslot_3',125,'0','0'])};__ez_fad_position('div-gpt-ad-pythonbaba_com-medrectangle-3-0');<span class="ezoic-ad medrectangle-3 medrectangle-3-multi-125 adtester-container adtester-container-125" data-ez-name="pythonbaba_com-medrectangle-3"><span id="div-gpt-ad-pythonbaba_com-medrectangle-3-0_1" ezaw="290" ezah="250" style="position:relative;z-index:0;display:inline-block;padding:0;min-height:250px;min-width:290px" class="ezoic-ad"><script data-ezscrex="false" data-cfasync="false" type="text/javascript" style="display:none">if(typeof ez_ad_units != 'undefined'){ez_ad_units.push([[250,250],'pythonbaba_com-medrectangle-3','ezslot_4',125,'0','1'])};__ez_fad_position('div-gpt-ad-pythonbaba_com-medrectangle-3-0_1'); &lt;/p&gt; &lt;h5&gt;&lt;span style="color:#339966"&gt;&lt;strong&gt;Output:&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;/h5&gt; &lt;pre class="EnlighterJSRAW" data-enlighter-theme="classic"&gt;The last two digit for the number: 23457 is: 57 &lt;/pre&gt; &lt;h5&gt;&lt;/h5&gt; &lt;script&gt; jQuery(document).ready(function($){ wizard_accordion = $("#accordions-496.accordions .items").accordion({ event: "click", collapsible:true, heightStyle: "content", animate: ("linear", 1000), navigation: true, active: 999999, changestart: function(event, ui) { child.accordion("activate", false); } }); var child = $(".child-accordion .items").accordion({ active:true, heightStyle: "content", collapsible: true, animated: "swing", }); $(".previous, .next").click(function () { var index = 0; console.log("gCurrentIndex:"+gCurrentIndex); console.log("index:"+index); if ($(this).hasClass("next")) { index = gCurrentIndex + 1; if (index &gt; ACCORDION_PANEL_COUNT ) { index = ACCORDION_PANEL_COUNT; } } else { index = gCurrentIndex - 1; if (index &lt; 0) { index = 0; } } wizard_accordion.accordion("option", "active", index); }); }) &lt;/script&gt;&lt;style type="text/css"&gt; #accordions-496 { text-align: left; } #accordions-496{ background: url() repeat scroll 0 0; padding: 0; } #accordions-496 .accordions-head{ background:rgba(130,36,227, 1) none repeat scroll 0 0; margin:1px; padding:10px; } #accordions-496 .accordions-head-title{ color:#ffffff; font-size:20px; font-family:; } #accordions-496 .accordions-head-title-toogle{ color:#ffffff; font-size:20px; } #accordions-496 .accordions-head:hover .accordions-head-title{ color:#000000; } #accordions-496 .ui-state-active{ background: #81d742; } #accordions-496 .accordion-content{ background:rgba(255,255,255,1) none repeat scroll 0 0; color:#333333; font-size:13px; font-family:; margin:0; padding:10px; } #accordions-496 .accordion-icons{ color:#565656; font-size:16px; padding:0px; background: ; } #accordions-496 .accordions-head:hover .accordion-icons{ color:#999; } #accordions-496 .expand-collapse{ } @media screen and (min-width: 1200px) { #accordions-496 { width: 100%; } } @media screen and (max-width:1199px) and (min-width: 768px) { #accordions-496 { width: 100%; } } @media screen and (max-width: 767px) { #accordions-496 { width: 100%; } } </p><div id="accordions-496" class="accordions accordions-themes flat accordions-496"> <div class="top-navs"> </div> <div class="items"> <div post_id="496" header_id="header-1577189209868" id="header-1577189209868" class="accordions-head head1577189209868" toogle-text="" main-text="Python Code Editor Online - Click to Expand"> <div class="accordion-icons left accordion-plus fa fas fa-chevron-up"></div> <div class="accordion-icons left accordion-minus fa fas fa-chevron-down"></div> <div id="header-text-1577189209868" class="accordions-head-title">Python Code Editor Online - Click to Expand</div> </div><div class="accordion-content content1577189209868"> <p><iframe width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen src="https://trinket.io/embed/python3/a5bd54189b" class="lazyload"></iframe></p> </div> </div> </div> <p>&nbsp;</p> <h3><span class="ez-toc-section" id="Python_code_implementation_using_function"></span><strong><span style="color:#ff0000">Python code implementation using function</span></strong><span class="ez-toc-section-end"></span></h3> <p><strong>In this code, we will pass the number to a function that will extract and return the last two digits of a number.</strong></p> <h5><span style="color:#339966"><strong>Code:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="atomic"># Python code to extract last two digits of a number using Function def last_2_digits(number): a_string = str(a) a_length = len(a_string) c = a_string[a_length - 2: a_length] return int(c) a = 10938 print("The last two digit for the number: ", a, " is: ", last_2_digits(a)) </pre> <p>&nbsp;</p> <h5><span style="color:#339966"><strong>Output:&nbsp;</strong></span></h5> <pre class="EnlighterJSRAW" data-enlighter-theme="classic">The last two digit for the number: 10938 is: 38 </pre> <h5></h5> <script> jQuery(document).ready(function($){ wizard_accordion = $("#accordions-496.accordions .items").accordion({ event: "click", collapsible:true, heightStyle: "content", animate: ("linear", 1000), navigation: true, active: 999999, changestart: function(event, ui) { child.accordion("activate", false); } }); var child = $(".child-accordion .items").accordion({ active:true, heightStyle: "content", collapsible: true, animated: "swing", }); $(".previous, .next").click(function () { var index = 0; console.log("gCurrentIndex:"+gCurrentIndex); console.log("index:"+index); if ($(this).hasClass("next")) { index = gCurrentIndex + 1; if (index > ACCORDION_PANEL_COUNT ) { index = ACCORDION_PANEL_COUNT; } } else { index = gCurrentIndex - 1; if (index < 0) { index = 0; } } wizard_accordion.accordion("option", "active", index); }); }) </script><style type="text/css"> #accordions-496 { text-align: left; } #accordions-496{ background: url() repeat scroll 0 0; padding: 0; } #accordions-496 .accordions-head{ background:rgba(130,36,227, 1) none repeat scroll 0 0; margin:1px; padding:10px; } #accordions-496 .accordions-head-title{ color:#ffffff; font-size:20px; font-family:; } #accordions-496 .accordions-head-title-toogle{ color:#ffffff; font-size:20px; } #accordions-496 .accordions-head:hover .accordions-head-title{ color:#000000; } #accordions-496 .ui-state-active{ background: #81d742; } #accordions-496 .accordion-content{ background:rgba(255,255,255,1) none repeat scroll 0 0; color:#333333; font-size:13px; font-family:; margin:0; padding:10px; } #accordions-496 .accordion-icons{ color:#565656; font-size:16px; padding:0px; background: ; } #accordions-496 .accordions-head:hover .accordion-icons{ color:#999; } #accordions-496 .expand-collapse{ } @media screen and (min-width: 1200px) { #accordions-496 { width: 100%; } } @media screen and (max-width:1199px) and (min-width: 768px) { #accordions-496 { width: 100%; } } @media screen and (max-width: 767px) { #accordions-496 { width: 100%; } }

Python code implementation using Classes

In this code, we will implement a class that contains a function to return the last two digits of a number. We will create an object and will call the function using object.function(), the will return the reverse integer number.

Code: 
# Python code to extract last two digits of a number using Classes

class Last_2_Digit_class(object):
    def last_2_digits(self, number):
        a_string = str(number)
        a_length = len(a_string)
        c = a_string[a_length - 2: a_length]
        return int(c)


a = 6560234

#Create an Object
last_2 = Last_2_Digit_class()

#Call the function using Object
print("Last 2 digit for number: ", a, " is: ", last_2.last_2_digits(a))
Output: 
Last 2 digit for number:  6560234  is:  34

Enjoy Python Code By Pythonbaba 🙂

Learn Python Programming at PythonBaba.com

Our team loves to write in Python, Linux, Bash, HTML, CSS Grid, CSS Flex, and Javascript. We love to write technical articles.

Currently exploring Data Science, Machine learning and Artificial intelligence.

How do I print last 2 digits in Python?

“get last 2 digits in string python” Code Answer.
>>>mystr = "abcdefghijkl".
>>>mystr[-4:].
'ijkl'.
>>>mystr[:-4] #to select all but last 4 characters..
'abcdefgh'.

How do you print the last digit of an integer in Python?

How to Get the Last Digit of a Decimal in Python.
pi = 3.141..
last_digit = int(repr(pi)[-1]).
print(f"The last digit of {pi} is {last_digit}").

How do you print last two digits?

The program output is also shown below..
/* * C Program to Extract Last two Digits of a given Year. #include <stdio.h> int main() { int year, yr; printf("Enter the year "); scanf("%d", &year); yr = year % 100; printf("Last two digits of year is: %02d", yr); ... .
Output: Enter the year 2012 Last two digits of year is: 12..

How do I get the last 3 digits of a number in Python?

Use the modulo operator to get the last 3 digits of a number, e.g. last_three = number % 1000 . The modulo operator will return the last 3 digits of the number by calculating the remainder of dividing the number by 1000. Copied!