In Python, one of the most broadly used programming languages today, it’s easy to perform diverse operations like splitting a three-digit number. This quick tutorial will instruct you on how to split a three-digit number into individual digits in Python. You will discover the process is straightforward, and the ideas you learn will build a foundation for other Python tasks.
Step 1: Declare the Three-Digit Number
The first stage is to define your three-digit number using the variable ‘num’. Here is an example:
1 |
num = 397 |
Step 2: Split the Number
Now it’s time to split the number into individual digits. This can be achieved by taking the remainder and integer portion of the number when divided by 10. Below is the Python code for the same:
1 2 3 |
num1 = num // 100 num2 = (num - num1 * 100) // 10 num3 = num - num1 * 100 - num2 * 10 |
Step 3: Print the Individual Digits
Lastly, let’s print the individual digits using Python’s print function. See the code below:
1 |
print("The digits are : ", num1, num2, num3) |
Full Python Code
Below is the entire Python Code that splits a three-digit number.
1 2 3 4 5 6 7 |
num = 397 num1 = num // 100 num2 = (num - num1 * 100) // 10 num3 = num - num1 * 100 - num2 * 10 print("The digits are : ", num1, num2, num3) |
Expected Output
The digits are: 3 9 7
If you wish to delve deeper into Python, you might find the Python Documentation beneficial, a comprehensive guide to all aspects of the language.
Conclusion
Whether you are new to Python or a seasoned developer, breaking down numbers into individual digits is a common task that you may find yourself needing to perform. As we have demonstrated in this tutorial, Python provides a straightforward method for disassembling numbers visually and programmatically. We hope that you now feel confident in splitting a three-digit number into its individual digits in Python.