DEV Community

Kiruthiga S
Kiruthiga S

Posted on

Python

22)Count

n=int(input("Enter a number: "))
count=0
while(n>0):
    count+=1
    n=n//10
print(f'count of a digit : {count}')
Enter fullscreen mode Exit fullscreen mode

Output:
Enter a number: 12345
count of a digit : 5

What does the f do?
The f stands for formatted string. It lets you insert variables directly inside a string using {}.

name = "Alice"
age = 20
print(f"My name is {name} and I am {age} years old.")
Enter fullscreen mode Exit fullscreen mode

Output: My name is Alice and I am 20 years old.

Sum

n=int(input("Enter a number: "))
sum=0
while n>0:
    sum=sum+(n%10)
    n=n//10
print(f'sum of digit: {sum}')  
Enter fullscreen mode Exit fullscreen mode

Output:
Enter a number: 12345
sum of digit: 15

Reverse

n=int(input("Enter a number: "))
rev=0
while n>0:
    rev=rev*10+(n%10)
    n=n//10
print(f'rev of digit: {rev}')  
Enter fullscreen mode Exit fullscreen mode

Output:
Enter a number: 12345
rev of digit: 54321

Top comments (0)