Contents

## String Manipulationclass 11Important QuestionsComputer Science with Python

### Q 1. What is a string slice? How is it useful ?

#### cbse class 11 computer science notes 2021

ans. A sub-part or a slice of a string, say s, can be obtained using s[n:m] where n and m are integers. Python returns all the characters at indices n, n+1, n+2…m-1 e.g.,
‘ Well done ‘ [1:4] will give ‘ ell ‘

### Q 2. Figure out the problem with following code fragment.

1. s1 = ‘must’
2. s2 = ‘try’
3. n1 = 10
4. n2 = 3
5. print(s1 + s2)
6. print(s2 * n2)
7. print(s1 + n1)
8. print(s2 * s1)

ans. The problem is with line 7 and 8.

• line 7 – print s1 + n1 will cause error because s1 being a string cannot be concatenated with a number n1.
• Line 8 – print(s2 * s1) will cause error because two string cannot be used for replication.

### Q 3. Consider the following code :string = input(“Enter a string :”)count = 3while True :if string == ‘a’ :string = string[2:]elif string[-1] == ‘b’ :string = string [:2]else :count+=1breakprint(string)print(count)what will be the output produced, if the input is : (i) aabbcc (ii) aaccbb (iii) abcc ?

ans.
(a) bbcc (b) cc (c) cc
4 4 4

### Q 4. Consider the following code :Inp =input( “please enter a string :”)while len(Inp) <=4 :if Inp[-1] == ‘z’ :Inp = Inp[0:3] = ‘c’elif ‘a’ in Inp :Inp = Inp + ‘bb’elif not int(Inp) :Inp = ‘1’ + Inp[1:] + ‘z’else :Inp = Inp + ‘‘ print(Inp) What will be the output produced if the input (i) 1bzz, (ii) ‘1a’ (ii) ‘abc’ (iv) ‘0xy’, (v) ‘xyz’ ?

ans.

(i) 1bzc
(ii) 1bb**
(iii) endless loop because ‘a’ will always remain at index 0 and condition 3 will be repeated endlessly.
(iv) 1Xyc*
(v) Raises an error as Inp cannot be converted to int.

(python string manipulation important questions)

### Q 5. 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”)
else :
print(string, “is a palindrome”)

### Q 6. Write a program that reads a string and displays the longest substring of the given string having just the consonants.

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 :
maxlength = lensub
sub = ‘ ‘
lensub = 0
else :
sub += string[a]
lensub = len(sub)
a += 1
print(“Maximum length consonant substring is :” , maxsub, end = ‘ ‘)
print9″with” , maxlength, “characters”)

### Q 7. Write a program that reads a string and 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 = ” “
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 8. 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 your email id :”)
domain = ‘@edupillar.com’
ledo = len(domain) £ ledo – length of domain
lema = len(email) £ lema – length of email
sub = email[lema-ledo :]
if sub == domain :
if ledo != lema :
print(“It is valid email id”)
else :
print(“This is invalid email id – contain just the domain name.”)

python string manipulation important questions

These Important Questions are based on latest CBSE Board Examination pattern.