how to find and get first and last two digits of a number using python

Spread the love

We are basically divided this article in Two part

  1. How to find first two digits of a number in python
  2. How to find last two digits of a number in python

How to find first two digits of a number in python


# how to find and get first and last two digits of a number using python
def firstDigit(n) :
 
    # Remove last digit from number
 till only one digit is left
    while n >= 10:
        n = n / 10;
     
    # return the first digit
    return int(n)
 
# Code for Find the last digit
def lastDigit(n) :
 
    # return the last digit
    return (n % 10)
 
# Driver Code
n = 14223;
print(firstDigit(n), end = " ")
print(lastDigit(n))

OUTPUT:

1 3

Python code for find and get last two digits of a number using python

# code for extract last two number using python

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 = 14343

#Create an Object
last_2 = Last_2_Digit_class()

#Call the function using Object
print("Check last two digits: ", a, " is: ", last_2.last_2_digits(a))

OUTPUT

Check last two digits: 4 3

Summary :

In this article we saw how to find and get first and last two digits of a number using python so about this article you have any query then free to ask me

Also read :

  1. Trace phone number using python

how to find and get first and last two digits of a number

This video help to you how practically without using any coding method find last to digits of complex number

sachin Pagar

Mr. Sachin Pagar is an experienced Embedded Software Engineer and the visionary founder of pythonslearning.com. With a deep passion for education and technology, he combines technical expertise with a flair for clear, impactful writing.

Leave a Reply