In this tutorial, we will learn how to print the tens digit of a number using Python. Python is an easy-to-learn, powerful programming language, making it an excellent choice for beginners and experts alike. By the end of this tutorial, you will be able to extract the tens digit from any given number efficiently.
Step 1: Accept user input for the number
First, we need to take a number as input from the user. We can use the input()
function to accept user input and store it in a variable.
1 |
number = int(input("Enter a number: ")) |
The input()
function returns a string, so we need to convert it to an integer using int()
function.
Step 2: Find the tens digit
Once we have the user’s number, we can perform simple arithmetic operations to extract the tens digit. To find the tens digit:
- Divide the number by 10 (integer division using
//
) to remove the last digit. - Get the remainder when the result is divided by 10 (using
%
).
1 |
tens_digit = (number // 10) % 10 |
Step 3: Print the tens digit
Now that we have calculated the tens digit, we just need to print it using the print()
function.
1 |
print("The tens digit is:", tens_digit) |
Full Code:
1 2 3 |
number = int(input("Enter a number: ")) tens_digit = (number // 10) % 10 print("The tens digit is:", tens_digit) |
Example and Output:
Enter a number: 12345 The tens digit is: 4
Conclusion
In this tutorial, we have learned how to print the tens digit of a number using Python. With this simple technique, we can easily extract specific digits from any given number. Now, go ahead and start practicing this technique with various numbers to get a better understanding of how it works.