Python String Programs


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



4 views0 comments

Recent Posts

See All
 

9555593818

Thakurdwara, Uttar Pradesh 244601, India

  • Facebook
  • Twitter
  • LinkedIn