How to Count Number of Digits in Python


This example tells how to count the number of digits for a given number in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

n = int(input("Please input a number: "))
if n >= 0:
    n = str(n)
    a = len(n)
    print("The number of digits in the number are:", a)
else:
    print("Your input is negative.")

Output:

Please input a number: 123456
The number of digits in the number are: 6

Please input a number: -123
Your input is negative.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement