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



10 views0 comments

Recent Posts

See All

Python program to add one or more element in List

Program: #Append() is used for adding one element in List at the end of list. X=[9,18,27] X.append(36) print(X) output: [9, 18, 27, 36] #Insert () is used for add element at any given position in the

Python program to remove elements of a given list

Program: n=[9,18,27,36,45,54,63] print("List=",n) i=int(input("Enter the position of elements to delete : ")) del n[i] print("List after deletion=",n) del n[:2] print("List after deleting first two el

Python program to create a list of 5 numbers

Python program to create a list of 5 numbers. Numbers entered by User. n=[] for i in range(5): j=int(input("Enter number: ")) n.append(j) print("The List is", n) Output: Enter number: 3 Enter number: