Python String Programs
top of page

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



23 views0 comments

Recent Posts

See All

Python program to calculate the factorial of a number

Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument. This program is most for O Level Practical Exam so read it care

NumPy program to find the most frequent value in the list.

This program is most for O Level Practical Exam so read it carefully. Program: import numpy as np x = np.array([1,2,3,4,5,7,2,1,1,1,8,9,1]) print(x) print("most frequent value in the list:") print(np.

bottom of page