Hướng dẫn how do i print the last 2 digits in python? - làm cách nào để in 2 chữ số cuối trong 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

Hướng dẫn how do i print the last 2 digits in python? - làm cách nào để in 2 chữ số cuối trong python?

BathshebaBathsheba

229k33 gold badges353 silver badges473 bronze badges

0

To get the last 2 digits of

 abs(num) % 100 
0 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

Hướng dẫn how do i print the last 2 digits in python? - làm cách nào để in 2 chữ số cuối trong python?

Israel UntermanIsrael Unterman

12.7k4 gold badges27 silver badges34 bronze badges

Simpler way to extract last two digits of the number (less efficient) is to convert the number to

 abs(num) % 100 
1 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

 abs(num) % 100 
2 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

Hướng dẫn how do i print the last 2 digits in python? - làm cách nào để in 2 chữ số cuối trong python?

Moinuddin QuadriMoinuddin Quadri

44.9k12 gold badges93 silver badges119 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

 

Python code implementation without user defined functions & classes

Code: 
# 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)