How do you print the number of digits in python?

Example 1: Count Number of Digits in an Integer using while loop

num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print["Number of digits: " + str[count]]

Output

Number of digits: 4

In this program, the while loop is iterated until the test expression num != 0 is evaluated to 0 [false].

  1. After the first iteration, num will be divided by 10 and its value will be 345. Then, the count is incremented to 1.
  2. After the second iteration, the value of num will be 34 and the count is incremented to 2.
  3. After the third iteration, the value of num will be 3 and the count is incremented to 3.
  4. After the fourth iteration, the value of num will be 0 and the count is incremented to 4.
  5. Then the test expression is evaluated to false and the loop terminates.

Example 2: Using inbuilt methods

num = 123456
print[len[str[num]]]

Output

6

In the above example, we first convert the integer value into string by using str[]. Then, we find the length of the string using len[].

I have some floating point values in Python. I would like to print them so that the have the same number of digits [sum of the integer and decimal parts]

For example considering the two numbers:

a = 12.123456
b = 123.1234567

I would like to print their value formatted as follows:

12.1234
123.123

So that they have the same length.

One simple way is the following:

if [val>100]:
    print["%0.3f" % val]
else:
    print["%0.4f" % val]

Is there a smarter way to control both the number of integer and decimal digits at the same time in python so that the resulting string is constant?

This is a Python Program to count the number of digits in a number.

Problem Description

The program takes the number and prints the number of digits in the number.

Problem Solution

1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and increment the count each time a digit is obtained.
3. Print the number of digits in the given integer.
4. Exit.

Program/Source Code

Here is source code of the Python Program to count the number of digits in a number. The program output is also shown below.

n=int[input["Enter number:"]]
count=0
while[n>0]:
    count=count+1
    n=n//10
print["The number of digits in the number are:",count]

Program Explanation

1. User must first enter the value of the integer and store it in a variable.
2. The while loop is used and the last digit of the number is obtained by using the modulus operator.
3. Each time a digit is obtained, the count value is incremented.
4. This loop terminates when the value of the number is 0.
5. The total count of the number of digits is printed.

Runtime Test Cases

 
Case 1:
Enter number:123
The number of digits in the number are: 3
 
Case 2:
Enter number:1892
The number of digits in the number are: 4

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

How do you calculate digits in Python?

Python Program to Count the Number of Digits in a Number.
Take the value of the integer and store in a variable..
Using a while loop, get each digit of the number and increment the count each time a digit is obtained..
Print the number of digits in the given integer..

How do you count the number of digits in a string in Python?

To find numbers from a given string in Python we can easily apply the isdigit[] method. In Python the isdigit[] method returns True if all the digit characters contain in the input string and this function extracts the digits from the string. If no character is a digit in the given string then it will return False.

How do you print the number of digits in a number?

We will create a C program to count the number of digits using functions..
#include .
int main[].
int num; // variable declaration..
int count=0; // variable declaration..
printf["Enter a number"];.
scanf["%d",&num];.
count=func[num];.

Chủ Đề