In this python tutorial, you will learn how to Count Number of Digits in an Integer using the while loop, inbuilt methods and the assignment operators of the python programming language.
How to Count Number of Digits in an Integer?
Let’s take a look at the first source code , here the values are given as input by the user in the code, the assignment operators along with the while loop carry out the function.
RUN CODE SNIPPET#Python Program to Count Number of Digits in an Integer using while loop num =int(input("Enter the integer: ")) count = 0 while num != 0: num //= 10 count += 1 print("\nNumber of digits: " + str(count))
INPUT:
979203580462
OUTPUT:
Enter the integer: Number of digits: 12
- Here we give the user the option to enter the values and the input values are scanned using the
input
function and are stored in the variablenum
with the statements/strings("Enter the integer: ")
 , we use theint
 function and declare the input value as an integer value. - We assign the variable
count
with the integer0
. - Next, we declare a
while
loop with the conditionnum != 0
 followed by a:
, after which we declare the conditionnum //= 10
wherecount += 1
. - In this code, the while loop is iterated until the test expression
num != 0
is equal to0
. During each iteration,num
will be divided and thecount
is incremented by one. When the test expression is evaluated to false and the loop terminates. - In the STDIN section of the code editor the input values are entered.
- The final output values are displayed with the statement
("\nNumber of digits: " + str(count))
, using theprint
function, where the+ str(count)
returns and displays the string version of thecount
value.
Let’s take a look at the second source code , here the values are given as input by the user in the code, inbuilt methods and print statement carry out the function.
RUN CODE SNIPPET#Python Program to Count Number of Digits in an Integer using inbuilt methods num =int(input("Enter the integer: ")) print("\nNumber of digits:",(len(str(num))))
INPUT:
979203580462
OUTPUT:
Enter the integer: Number of digits: 12
- Here we give the user the option to enter the values and the input values are scanned using theÂ
input
function and are stored in the variablenum
with the statements/strings("Enter the integer: ")
 , we use theint
 function and declare the input value as an integer value. - In the STDIN section of the code editor the input values are entered.
- TheÂ
print
function will display the statement("\nNumber of digits:",(len(str(num))))
, where the function(len(str(num)))
will display the length of the value in string format which the variablenum
holds .
NOTE:
- The != operator is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal.
- The += operator know as the addition assignment operator lets you add two values together and assign the resultant value to a variable.
- The //= operator know as the double division assignment operator lets you divide two values together and assign the resultant value to a variable.
- The str() function in python returns the string version of the object.
- The While true loop in python runs without any conditions until the break statement executes inside the loop.
- The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
- The len() function is an inbuilt function in Python programming language that returns the length of the string.
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.
- The print statement/string to be displayed in enclosed in double quotes.
- The input() function allows a user to insert a value into a program, it returns a integer value.
- The statement for the input function are enclosed in single quotes and parenthesis.