Worksheet - Strings in Python
Worksheet - Strings in Python
Worksheet - Strings in Python
1) Python does not support a character type; a single character is treated as strings of
length one.
a. False b. True c. Error d. None of the mentioned
2) What will be the output?
str1="6/4"
print("str1")
A. error B. 6/4 C. 1.5 D. str1
3) Which of the following will result in an error?
str1="python"
A. print(str1[2]) B. str1[1]="x" C. print(str1[0:9]) D. Both (b) and (c)
4) What will be the output of below Python code?
str1="Information"
print(str1[2:8])
A. format B. formati C. nforma D. ormat
5) Which of the following will give "Simon" as output? If str1="John,Simon,Aryan"
A. print(str1[-7:-12]) B. print(str1[-11:-7])
C. print(str1[-11:-6]) D. print(str1[-7:-11])
6) What would the following code print?
Mali = 5
print("Mali" + " is " + str(Mali))
A. Mali is Mali B. Mali is 5 C. 5 is Mali D. 5 is 5
7) What is printed by the following statements?
s = "python rocks"
print(s[7:11] * 3)
A. rockrockrock B. rock rock rock
C. rocksrocksrocks D. TypeError, you cannot use multiplication with slicing.
8) What will be printed when the following executes?
str = "This is fun"
str = str[5]
print(str)
A. I B. ' ' C. is fun D. This is fun
9) What will be the output of the following Python statement? >>>"a"+"bc"
a) a bc b) bc c) bca d) abc
10) What arithmetic operators cannot be used with strings?
a) + b) * c) – d) All of the mentioned
11) What will be the output of the following Python statement? >>>print('new' 'line')
a) Error b) Output equivalent to print „new\nline‟
c) newline d) new line
12) What will be the output of the “hello” +1+2+3?
a) hello123 b) hello c) Error d) hello6
13) s=”hello” what will be the return value of type(s)?
a) int b) bool c) str d) String
14) What function do you use to read a string?
a) input(“Enter a string”) b) eval(input(“Enter a string”))
c) enter(“Enter a string”) d) eval(enter(“Enter a string”))
15) Which line of code produces an error?
a) “Python” + “12” b) „Python‟ + 16 c) 3 + 7 d) „Python‟ + “21”
16) Consider the string str=”Green Revolution” choose the correct statements in the python
to implement the following in question,Display last four characters
(a) str[-4: ] (b) str[ :-4: ] (c) str[ : : ] (d) str[ : : -4]
17) How many times is the word “HELLO” printed in the following statement?
s=‟python rocks‟
for ch in s[3:8]:
print(„Hello‟ )
(a) 6 (b) 5 (c) infinite (d) 8
18) (A) Assertion : b = “Hello, World!” print(b[:5]) will give output “Hello”
(R) Reason : This will give get the characters from start position(5 not included)
(a) A is true but R is false (b) A is true and R is correct explanation of A
(c) A and B both are false (d) A is true but R is not correct explanation of A
19) Statement (1) As we know that strings are immutable. We cannot delete or remove the
characters from the string.
Statement (2) But we can delete the entire string using the del keyword
(a) Statement 1 and Statement 2 both are true
(b) Both statement 1 and 2 are false
(c) statement 1 is false and Statement 2 is true
(d) statement 1 is true and Statement 2 is false
20) (A) Assertion str1=”Hello” and str1=”World” then print(str1*3) will give error
(R) Reason : * replicates the string hence correct output will be HelloHelloHello
(a) A is true but R is false (b) A is true but R is not correct explanation of A
(c) A and B both are false (d) A is false and R is correct
21) What is the output of the following program?
my_string = „Python'
for i in range(my_string):
print(i)
a) 0 1 2 3 … 12 b) No Output c) Error d) Python
22) What is the output of the following program?
x = 123
for i in x:
print(i, end=" ")
a. 1 2 3 b. 1 1 c. Error d. no output
23) For strings, the symbol + means ______.
a. addition b. swap c. concatenation d. slicing
24) What is the output of the following code?
str1='101'
x=int(str1)
z=x+400
print(z)
a. 101400 b. x400 c. 501 d. none of above
25) The index of a string begins with ________.
a. 1 b. 0 c. a d -1
26) What will the output be from the following code?
print("Hello" + str(2) + "world!")
Hello world! Hello world!
Hello2world!
Hello Hello world! world!
SyntaxError
27) Joining elements together to make a string is called what?
a. Combining b. Connecting c. Concatenation d join()
28) In python, string + and * represents which of the following operations?
a. concatenation, Replication b. Addition, Multiplication
c. Neither (a) or (b) d. Both (a) or (b)
29) Which of the following is not a Python legal string operation?
a. „abc‟ + „ABC‟ b. „abc‟*3 c. „abc‟+3 d. none of above
30) Which of the following is the correct syntax of string slicing?
a. s[start:end] b. s[start:step] c. s[step:end] d. s[step:start]
31) How many times is the word „Python‟ printed in the following statement?
s=‟I love Python‟
for ch in s[3:8]:
print(„python‟)
a. 11 b. 8 c. 3 d. 5
32. To concatenate means to ______.
a. replicate b. join c. split d. multiply
33. What is the correct python code to display the last four characters of „Digital India‟?
a. s[-4:] b. s[4:] c. s[*4] d. s[-1:4]
34. which operator use to compare two strings.
a. >* b. + c. === d ==
35. In the membership operator, It returns true if a character/ substring exists in
the given string.
a. in operator b. not operator c. not in operator d. none of above
Worksheet – II Strings in Python
1) Write the output of the following.
>>> a = "Blog"
>>> a ='a'
>>> print(a)
a. Bloga b. aBlog c. Error d. a
2) What is the index value of „i‟ in string “Learning”
a. 5 b. 3 c. 6 d. 4
3) Index value in String should be of type __________.
a. Float b. Integer c. String d. Boolean
4) string traversal can be done using „for‟ loop only.(T/F)
a. True b. False
5) Which operator is used with integers as well as with strings?
a. / b. // c. ** d. *
6) Write the output of the following:
>>> 2 + '3'
a. 2 3 b. 23 c. SyntaxError d. TypeError
7) Which operator is used for string concatenation?
a. * b. // c. + d. –
8) Write the output of the following code:
s="blog"
for i in range(-1,-len(s),-1):
print(s[i],end="$")
a. g$o$l$b$ b. g$o$l$ c. Error d. None of the above
9) Write the output of the following code:
>>> s= "str"
>>> s1 = "string"
>>> print(s1 in s)
a. True b. False c. Error d. None of the above
10) Write the output of the following code:
>>> print("Amita" > "amit")
a. True b. False c. Error d. None of the above
11) What is the ASCII value of “A”?
a. 97 b. 66 c. 65 d. 96
12) Write the output of the following code:
>>> print("Str"[1:2])
a. t b. No Output c. Error d. None of the above
13) Which of the following function returns the ASCII/Unicode value character?
a. asc( ) b. ord( ) c. asci( ) d. ascii( )
14) Which of the following statements will also return the same result as given statement?
>>> print(“Computer” + “Computer”)
a. print(“Computer” , ” “, “Computer”) b. print(“Computer”,”Computer”)
c. print(“Computer” **2) d. print(“Computer” * 2)
15) A _________ can be created by enclosing one or more characters in single, double or
triple quote.
a. String b. List c. Tuple d. Dictionary
16) Characters in a string can be _________ enclosed in quotes.
a. Digits b. white space c. letter d. All of the above
17) Each individual character in a string can be accessed using a technique called _____.
a. Indexing b. Replication c. Concatenation d. None of the above
18) If we give index value out of the range then we get an ________.
a. ValueError b. SyntaxError c. IndexError d. Error
19) Write the output of the following.
str = “python”
print(str[2+1])
a. t b. h c. th d. Error
20) Python allows _____________ operations on string data type.
a. Concatenation b. Membership c. Slicing d. All of the above
21) Write the output of the following.
>>> str1 = „Hello World!‟
>>> „w‟ in str1
a. True b. False c. No d. None of the above
22) _____________ method is used to access some part of a string or substring.
a. Slicer b. Slicing c. Membership d. None of the above
23) str[5 : 11] will return ___________ characters.
a. 5 b. 6 c. 11 d. None of the above
24) print(„Welcome TO My Blog'[2:6] + „Welcome TO My Blog'[5:9]) will display _____.
a. lcomme T b. lcomme c. lcomme To d. None of the above
25) Select the wrong option in reference to the statement given below:
(operand1) * (operand2) No error
a. Both operands can be of type integer
b. operand1 can be of type integer and operand2 can be of type string
c. operand1 can be of type string and operand2 can be of type integer
d. Both operands can be of type string
Worksheet – III Strings in Python
1) The ____ function returns the exact copy of the string with the first letter in uppercase.
(a) find() (b) copy() (c) upper() (d) capitalize()
2) Find the output of the following.
>>> word=”green vegetables”
>>> print(word.find(„veg‟,2)
(a) 8 (b) 6 (c) 10 (d) 12
3) Which of the following will result in an error?
>>> str1="python"
A. print(str1[2]) B. str1[1]="x" C. print(str1[0:9]) D. Both (b) and (c)
4) Given a string example=”hello” what is the output of example.count(„l‟)
(a) 2 (b) 1 (c) None (d) 0
5) Which of the following will give "Simon" as output? If str1="John,Simon,Aryan"
A. print(str1[-7:-12]) B. print(str1[-11:-7])
C. print(str1[-11:-6]) D. print(str1[-7:-11])
6) What is the output of the following code?
>>> example=”helloworld”
>>> example[::-1].startswith(“d”)
(a) dlrowolleh (b) True (c) - (d) None
7) What is the output of the following?
>>> print(“xyyzxyzxzxyy”.count(„xyy‟, 2, 11))
(a) 2 (b) 0 (c) 1 (d) error
8) What is “Hello”.replace(“l”, “e”)
(a) Heeeo (b) Heelo (c) Heleo (d) None
9) What is the output of the following?
>>> print(“xyyzxyzxzxyy”.endswith(“xyy”, 0, 2))
(a) 0 (b) 1 (c) True (d) False
10) What is the output of the following? >>> print(„ab12‟.isalnum())
(a) True (b) False (c) None (d) Error
11) What is the output of the following? >>> print(„a B‟.isalpha())
(a) True (b) False (c) None (d) Error
12) What is the output of the following? >>> print( „ „.isdigit())
(a) True (b) False (c) None (d) Error
13) What is the output of the following? >>> print(„a@ 1,‟.islower())
(a) True (b) False (c) None (d) Error
14) What is the output of the following?
>>> print('abcd@efcd@ghcd'.split('@'))
(a) ['abcd', „@‟, 'efcd', „@‟, 'ghcd'] (b) ['abcd@', 'efcd@', 'ghcd']
(c) ['abcd', 'efcd', 'ghcd'] (d) Error
15) myTuple = (“Joe”, “Peter”, “Vicky”)
x = “#”.join(myTuple)
print(x) will produce output
(a) Joe#Peter#Vicky (b) #JoePeterVicky
(c) JoePeterVicky (d) JohnPete#Vicky#
16) What is the output of the following?
>>> print(„Ab!2‟.swapcase())
(a) AB!@ (b) ab12 (c) aB!2 (d) aB1@
17) Choose the correct function to get the character from ASCII number
(a) ascii(number) (b) char(number) (c) chr(number) (d) ord( „ a ‟)
18) Select the correct output of the following String operations.
>>> str1 = "my isname isisis jameis isis bond"
>>> sub = "is"
>>> print(str1.count(sub, 4))
(a) 5 (b) 6 (c) 7 (d) 0
19) Which method removes all the trailing whitespaces from the right of the string?
(a) strip( ) (b) remove( ) (c) lstrip( ) (d) rstrip( )
20) Which of the following is not a String built in functions?
a. isupper( ) b. lower( ) c. partition( ) d. swapcases( )
21) Write the output of the following:
>>> str1 = 'India is a Great is Country'
>>> str1.partition('is')
a. („India „, „is‟, „ a Great is Country‟) b. („India „, „is‟, „a Great‟, „is‟, „Country‟)
c. („India „, „is‟, „ a Great Country‟) d. Error
22) partition( ) function of string in python return the result in the form of ________.
a. String b. List c. Tuple d. Dictionary
23) Which of the following function returns True if the string is non-empty and has all
uppercase alphabets.
a. islower( ) b. isupper( ) c. Islower( ) d. istitle
24) Read the statements given below and identify the right option.
Statement A : Python has two membership operators „in‟ and „not in‟
Statement B : The „in‟ operator takes two strings and returns False if the first string
appears as a substring in the second string, otherwise it returns True .
a. Statement A is correct.
b. Statement B is correct.
c. Statement A is correct, but Statement B is incorrect.
d. Statement A is incorrect, but Statement B is correct.
25) Read the statements given below and identify the right option
Statement A : Python allows an index value to be negative also.
Statement B : Negative indices are used when we want to access the characters of
the string from left to right.
a. Statement A is correct.
b. Statement B is correct.
c. Statement A is correct, but Statement B is incorrect.
d. Statement A is incorrect, but Statement B is correct.