String Worksheet
Which of the following is not a Python legal string operation?
1. 'abc' + 'abc'
2. 'abc' *3
3. 'abc' + 3
4. 'abc'.lower()
Question 2
In Python string, + and * represent which of the following operations?
1. Concatenation, Replication
2. Addition, Multiplication
3. Neither (i) nor (ii)
4. Both (i) and (ii)
Question 3
Which of following is not a valid string operation?
1. Slicing
2. Concatenation
3. Repetition
4. Floor
Question 4
How many times is the word "Python" printed in the following statement?
s = 'I love Python'
for ch in s[3:8]:
print('Python')
1. 11 times
2. 8 times
3. 3 times
4. 5 times
Question 5
Which of the following is the correct syntax of String Slicing?
1. String_name [start : end]
2. String_name [start : step]
3. String_name [step : end]
4. String_name [step : start]
Question 6
What is the correct Python code to display the last four characters of "Digital India"?
1. str [-4 :]
2. str [4 :]
3. str [*4 :]
4. str [/4 : ]
Question 7
What will be the output of the following code?
str1= "I love Python."
strlen = len(str1)
print(strlen)
1. 9
2. 29
3. 14
4. 3
Question 8
What will be the output of the following code?
Str1= 'My name is Nandini '
Str2 = Str1[3:7]
strlen = len(Str2)
print(strlen)
1. 4
2. 14
3. 24
4. 34
Question 9
Which method removes all the trailing whitespaces from the right of the string?
1. tolower()
2. upper()
3. Istrip()
4. rstrip()
Question 10
To concatenate means to ............... .
1. replicate
2. join
3. split
4. multiply
Question 11
............... function will always return tuple of 3 elements.
1. index()
2. split()
3. partition()
4. strip()
Question 12
What will be the output of the following code?
A = 'Virtual Reality'
[Link]('Virtual', 'Augmented')
1. Virtual Augmented
2. Reality Augmented
3. Augmented Virtual
4. Augmented Reality
Question 13
What will be the output of the following?
print("ComputerScience".split("er", 2))
1. ["Computer", "Science"]
2. ["Comput", "Science"]
3. ["Comput", "erScience"]
4. ["Comput", "er", "Science"]
Question 14
What is the ASCII equivalent decimal no. for 'Y'?
1. 87
2. 88
3. 89
4. 90
Question 15
STR = "RGBCOLOR"
colors = list(STR)
How do we delete 'B' in given List colors?
1. del colors[2]
2. [Link]("B")
3. [Link](2)
4. All of these
Question 16
Which of the following will give "Simon" as output?
str1 = "Hari,Simon,Vinod"
1. print(str1[-7:-12])
2. print(str1[-11:-7])
3. print(str1[-11:-6])
4. print(str1[-7:-11])
Assertions and Reasons
Question 1
Assertion(A): S1 ='CoMPuter SciENce'
S1[0] = S1[0].lower()
The above code will generate error.
Reasoning(R): String is immutable by default. The contents of the string cannot be
changed after it has been created.
1. Both A and R are true and R is the correct explanation of A.
2. Both A and R are true but R is not the correct explanation of A.
3. A is true but R is false.
4. A is false but R is true.
Question 2
Assertion (A): print('INDIA'.capitalize())
This command on execution shall display the output as 'India'.
Reasoning (R): The capitalize() method returns a string where the first character is
uppercase and the rest is lower case.
1. Both A and R are true and R is the correct explanation of A.
2. Both A and R are true but R is not the correct explanation of A.
3. A is true but R is false.
4. A is false but R is true.
Question 3
Assertion (A): The process of repeating a set of elements in a string is termed as
replication.
Reasoning (R): Repetition allows us to repeat the given string using (*) operator along with
a number that specifies the number of replications.
1. Both A and R are true and R is the correct explanation of A.
2. Both A and R are true but R is not the correct explanation of A.
3. A is true but R is false.
4. A is false but R is true.
Question 4
Assertion (A): In Python, the strings are mutable.
Reasoning (R): In strings, the values are enclosed inside double or single quotes.
1. Both A and R are true and R is the correct explanation of A.
2. Both A and R are true but R is not the correct explanation of A.
3. A is true but R is false.
4. A is false but R is true.
Question 5
Assertion (A): Slicing a string involves extracting substring(s) from a given string.
Reasoning (R): Slicing does not require start and end index value of the string.
1. Both A and R are true and R is the correct explanation of A.
2. Both A and R are true but R is not the correct explanation of A.
3. A is true but R is false.
4. A is false but R is true.
Question 6
Assertion (A): The swapcase() function converts an input string into a toggle case.
Reasoning (R): The swapcase() function converts all uppercase characters to lowercase
and vice versa of the given string and returns it.
1. Both A and R are true and R is the correct explanation of A.
2. Both A and R are true but R is not the correct explanation of A.
3. A is true but R is false.
4. A is false but R is true.
Question 7
Assertion (A): Indexing in a string is defined as positive and negative indexing.
Reasoning (R): In forward and backward indexing, the indices start with 1st index.
1. Both A and R are true and R is the correct explanation of A.
2. Both A and R are true but R is not the correct explanation of A.
3. A is true but R is false.
4. A is false but R is true.
Question 8
Assertion (A): The following code snippet on execution shall return the output as: False
True
str1 "Mission 999"
str2 = "999"
print([Link](), [Link]())
Reasoning (R): isdigit() function checks for the presence of all the digits in an inputted
string.
1. Both A and R are true and R is the correct explanation of A.
2. Both A and R are true but R is not the correct explanation of A.
3. A is true but R is false.
4. A is false but R is true.
Question 1
How does '*' operator behave on strings?
Question 2
Explain function split() with an example.
Question 3
How many times is the word 'HELLO' printed in the following statement?
s = "python rocks"
for ch in s:
print ("Hello")
Question 4
Write the output of the following:
>>> x = "hello"
>>> print(x[1:-2])
Question 5(a)
Write the output of the following:
string = "Hello Madam, I love Tutorials"
substring = "Madam"
if [Link](substring) != -1:
print("Python found the substring!")
else:
print("Python did NOT find the substring!")
Question 5(b)
Write the output of the following:
s = "Strings in Python"
print([Link]())
print([Link]())
s6 = [Link]("in", "data type")
print(s6)
Question 6
Find the output of the following:
word = 'work hard'
result = [Link]('work')
print("Substring, 'work', found at index:", result)
result = [Link]('har')
print("Substring, 'har ' ,found at index:", result)
if ([Link]('pawan') != -1):
print("Contains given substring ")
else:
print("Doesn't contain given substring")
Question 7
Consider the following string mySubject:
mySubject = "Computer Science"
What will be the output of the following string operations?
(i) print (mySubject [0:len(mySubject)])
(ii) print (mySubject[-7:-1])
(iii) print (mySubject[::2])
(iv) print (mySubject [len (mySubject) -1])
(v) print (2*mySubject)
(vi) print (mySubject[::-2])
(vii) print (mySubject[:3] + mySubject[3:])
(viii) print ([Link]() )
(ix) print ([Link]('Comp') )
(x) print ([Link]() )
Question 8
Write the Python statement and the output for the following:
(a) Find the third occurrence of 'e' in 'sequence'.
(b) Change the case of each letter in string 'FuNcTioN'.
(c) Whether 'Z' exists in string 'School' or not.
Question 9
Consider the string str1 = "Global Warming".
Write statements in Python to implement the following:
(a) To display the last four characters.
(b) To replace all the occurrences of letter 'a' in the string with "*".
Question 10
What will be the output of the following code?
Text = "Mind@Work!"
ln = len(Text)
nText = ""
for i in range(0, ln):
if Text[i].isupper():
nText = nText + Text[i].lower()
elif Text[i].isalpha():
nText = nText + Text[i].upper()
else:
nText = nText + 'A'
print(nText)
Question 11
Input the string 'My School'. Write a script to partition the string at the occurrence of letter
'h'.
Example :
('My Sc', 'h', 'ool')
Question 12
Write a program to convert a string with more than one word into titlecase string where
string is passed as parameter. (Titlecase means that the first letter of each word is
capitalized.)
Example
Enter a string: python programming is fun
Python Programming Is Fun
Question 13
Write a program that takes a sentence as an input parameter where each word in the
sentence is separated by a space. The function should replace each blank with a hyphen
and then return the modified sentence.
Example:
Enter a sentence: Innovation drives progress
Innovation-drives-progress
Question 14
Write a script to partition the string 'INSTITUTE' at the occurrence of letter 'T'.
Example:
('INS', 'T', 'ITUTE')
Question 15
What will be the output of the following programming code?
str = "My Python Programming"
print (str[-5:-1])
print (str[1:5])
print (str[:-4])
print (str[0:])
print (str[:13-4])
print (str[:3])
Question 16
Write a program to count the number of each vowel in a given string.
Example:
Enter a string: The quick brown fox jumps over the lazy dog.
Vowel counts: {'a': 1, 'e': 3, 'i': 1, 'o': 4, 'u': 2}
Question 17
Write a program that reads a line, then counts how many times the word 'is' appears in the
line and displays the count.
Example:
Enter a line: The project is ambitious, and its success is dependent on how well it is
managed and executed.
The word 'is' appears 3 times.
Question 18
Write a program to remove 'i' (if any) from a string.
Example:
Enter a string: institute
String after removing 'i': nsttute