Class XI : Python String Programs

Question:1

Write a python program to get the following output:

#
##
###
####
#####

Solution:

str="#"
n=""
for i in range(5):
    n+=str
    print(n)

Question:2

Write a python script to take input for a string , count and print length of the string?

Solution:

n=input("Enter any string ")
#method :1: using len() function
n1=len(n)
print("length = ",n1)
#method : 2
c=0
for i in n:
    c=c+1
    print(i)
print("Length = ",c)

Output:

Enter any string hello
length = 5
h
e
l
l
o
Length = 5
>>>

Output:

Enter any string hello world
length = 11
h
e
l
l
o

w
o
r
l
d
Length = 11
>>>

Question:3

Write a python script to take input for a string count and print total lower case alphabets in the string?

n=input("Enter any string ")
c=0
for i in n:
    print(i)
    if(i>='a' and i<='z'):
        c=c+1
print("Total lower case alphabets = ",c)

Output:
Enter any string HEllo
H
E
l
l
o
Total lower case alphabets = 3
>>>

Question:4

Write a python script to take input for a string count and print total upper case alphabets in the string?

n=input("Enter any string ")
c=0
for i in n:
    print(i)
    if(i>='A' and i<='Z'):
        c=c+1
print("Total upper case alphabets = ",c)

Question:5

Write a python script to take input for a string count and print total digits in the string?

n=input("Enter any string ")
c=0
for i in n:
    print(i)
    if(i>='0' and i<='9'):
        c=c+1
print("Total digits = ",c)

Question:6

Write a python script to take input for a string count and print total vowels in the string?

n=input("Enter any string ")
c=0
vowels="aeiouAEIOU"
for i in n:
    print(i)
    if(i in vowels):
        c=c+1
print("Total vowels = ",c)

Question:7

Write a python script to take input for a string and print the following:
Total Length
Total alphabets
Total lower case alphabets
Total upper case alphabets
Total digits
Total spaces
Total special characters

n=input("Enter any string ")
c=0
a=0
la=0
ua=0
d=0
sp=0
spl=0
for i in n:
    print(i)
    if((i>='A' and i<='Z') or (i>='a' and i<='z')):
         a=a+1         
         if(i>='A' and i<='Z'):
             ua=ua+1
         else:
            la=la+1     
    elif(i>='0' and i<='9'):
        d=d+1
    elif(i==' '):
        sp=sp+1
    else:
        spl=spl+1
    c=c+1
print("Total Length = ",c)
print("Total alphabets = ",a)
print("Total lower case alphabets = ",la)
print("Total upper case alphabets = ",ua)
print("Total digits = ",d)
print("Total spaces = ",sp)
print("Total special characters = ",a)

Question:8

Write a python script to take input for a string, further take input for a sub string to search check and print whether the sub string is present in the string or not? And if present also print how many times it is present and its position?

n=input("Enter main string ")
n1=input("Enter sub string to search ")
len1=len(n)
len2=len(n1)
c=0 #total count how may time sub string is present
pos=0
i=0
while True:
    pos=n.find(n1,i,len1)
    if(pos!=-1):
        print("found at ",pos)
        c=c+1
        i=pos+len2
    else:
        break
    if(i>=len1):
        break
print("sub string ",n1," Found ",c," times")

Output:

nter main string HELLO
Enter sub string to search L
found at 2
found at 3
sub string L Found 2 times
>>>

 

Number Of Visitors

0238246