top of page

Python String Programs

  • Writer: Rajesh Singh
    Rajesh Singh
  • Feb 23, 2021
  • 1 min read

Write a program to input a sentence and count the number of words in it.

Program:

s=input("Enter the sentence: ")

k=0

for i in s:

if i.isspace():

k=k+1

else:

print(" Number of words=", k+1)

Output:

Enter the sentence: Rajesh Singh

Number of words= 2



Write a program to input a string and print its capital letters in small and small letters into capital.

Program:

s=input("Enter the sentence: ")

for a in s:

if a.islower():

print(a.upper(),end="")

else:

print(a.lower(),end="")


Output:

Enter the sentence:Rajesh

rAJESH


Write a python program to input a string and calculate the sum of the digits present in it.

Program:

s=input("Enter the sentence: ")

k=0

for i in s:

if i.isdigit():

k=k+int(i)

print("Sum=", k)

Output:

Enter the sentence: 6 Rajesh 8

Sum= 14



Recent Posts

See All

Comentarios


bottom of page