string manipulation in python class 11 programs

Contents show

string manipulation in python class 11 programs

string manipulation in python class 11 programs
string manipulation in python class 11 programs

Q1.program to read a string and display it in reverse order- display one character per line.

do not create a reverse string, just display in reverse order.

cbse class 11 computer science notes 2021

string manipulation in python class 11 programs


ans.

string1=input("enter a string:")
print("the",string1,"in reverse order is:")
length=len(string1)
for a in range(-1,(-length-1),-1):
           print(string[a])


sample run of above program is:

enter a string : python
the python in reverse order is :
n
o
h
t
y
p

Q2.program to rea a string and display it in form.
first character last character
second character second last character
. .

. .


ans.

string1=input("enter a string:")
length=len(string1)
i=0
for a in range(-1,(-length-1),-1):
print(string[i],"/t", string[a])
i+=1


sample run of above program is:

enter a string : python
p     n
y     o
t     h
h     t
o     y
n     p

Q3. program that prints the following pattern without using any nested loop
1 #
2 # #
3 # # #
4 # # # #
5 # # # # #


ans.

string = '#'
pattern = " " # empty string
for a in range(5):
pattern+=string
print(pattern)


sample run of the program is shown below:

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

Q 4. Program that reads a line and prints its statistics like :
1. Number of uppercase letters :
2. Number of lowercase letters :
3. Number of alphabets :
4. Number of digits :

ans.

line = input("Enter a line :")
lowercount = uppercount = 0
digitcount = alphacount = 0
for a in line :
       if a.islower() :
             lowercount += 1
       elif a.isupper() :
             uppercount += 1
       elif a.isdigit() :
             digitcount += 1
        if a.isalpha() :
            alpacount += 1
print("Number of uppercase letters :", uppercount)
print("Number of lowercase letters :", lowercount)
print("Number of alphabets :", alphacount)
print("Number of digits :", digitcount)


sample run of the program is :

Enter a line : Hello 123, ZIPPY zippy zap
Number of uppercase letters : 7
Number of lowercase letters : 11
Number of alphabets : 18
Number of digits : 3

Q 5. Program that reads a line and a substring. It should then display the number of occurrences of the given substring in the line.


ans.

line = input("Enter line :")
sub = input("Enter substring :")
length = len(sub)
start = count = 0
end = length
while True :
            pos = line.find(sub, start, end)
            if pos! = -1 :
                   count += 1
                   start = pos + lensub
            else :
                 break
             if start >= length :
                       break
print("No. of occurence of", sub, ':', count)


sample runs of above program is :


Enter line : jingle bells jingle bells jingle all the way
Enter substring : jingle
No. of occurrence of jingle : 3
================= Restart ==================
Enter line : jingle bells jingle bells jingle all the way
Enter substring : bells
No. of occurrence of jingle : 2

Q 6. Write a program that takes a string with multiple words and then capitalizes the letter of each word and forms a new string out of it.


ans.

string = input("Enter a string :")
length = len(string)
a = 0
end = length
string2 = ' ' #empty string
while a < length :
         if a == 0 :
              string2 += string[0].upper()
              a += 1
        elif (string[a] == ' ' and string[a+1] != ' ')
              string2 += string[a]
              string2 += string[a+1].upper()
              a += 2
        else :
              string2 += string[a]
              a += 1
print("original string :", string)
print("capitalized words string", string2)


Q 7. write a program that reads a string and checks whether it is a palindrome string or not.


ans.

string = input("Enter a string :")
length = len(string)
mid = length/2
rev = -1
for a in range(mid) :
       if string[a] == string[rev] :
              a += 1
              rev -= 1
        else :
             print(string, "is not a palindrome")
             break
else :
        print(string, "is a palindrome")

Q 8. Write a program that reads a string and displays the largest substring of the given string having just the consonants. (string manipulation in python)


ans.

string = input("Enter a string :")
length = len(string)
maxlength = 0
maxsub = ' '       #empty string
sub = ' '               #empty string
lensub = 0
for a in range(length) :
          if string[a] in 'aeiou' or string[a] in 'AEIOU' :
                if lensub > maxlength :
                       maxsub = sub
                       maxlength = lensub
                       sub = ' '
                       lensub = 0
                 else :
                       sub += string[a]
                       lensub = len(sub)
                       a += 1
print("Maximum length consonant substring is :", maxsub, end = ' ')
print("with", maxlength, "characters")

Q 9. write a program that reads and then prints a string that capitalizes every other letter in the string e.g., passion becomes pAsSiOn


ans.

string = input("Enter a string :")
length = len(string)
print("original string :", string)
string2 = ' ' #empty string
for a in range(0, length, 2) :
                string2 += string[a]
                if a < (length-1) :
                      string2 += string[a+1].upper()
print("Alternatively capitalized string :", string2)

Q 10. write a program that reads email-id of a person in the form of a string and ensures that it belongs to domain @edupillar.com. (Assumption : No invalid characters are there in email-id).


ans.

email = input("Enter you email id :")
domain = '@edupillar.com'
ledo = len(domain) # ledo - length of domain
lema = email[lema-ledo :] # lema - length of email
if sub == domain :
       if ledo != lema :
             print("it is valid email id")
        else :
             print("This is invalid email id - contain just the domain name.")
else :
print(This email-id is either not valid or belongs to some other domain.")

String manipulation in python in class 11 notes

string manipulation in python