Quiz: String

To View Tricks: Login Required

Number of Questions: 10

Question: 6 -

What will be the output of below Python code?

str1="poWer"
str1.upper()
print(str1)

Options:
  1. power

  2. poWer

  3. Power

  4. POWER

  5. Answer:

    poWer

    Solution:

    str1.upper() returns the uppercase of whole string str1. However,it doesnot change the string str1. So, output will be the original str1.


Question: 7 -

What will following Python code return?

str1="Stack of books"
print(len(str1))

Options:
  1. 13

  2. 16

  3. 14

  4. 15

  5. Answer:

    14

    Solution:

    len() returns the length of the given string str1, including spaces and considering " " as a single character.


Question: 8 -

What will the below Python code will return?

list1=[0,2,5,1]
str1="7"
for i in list1:
str1=str1+i
print(str1)

Options:
  1. 15

  2. Error

  3. 7

  4. 70251

  5. Answer:

    Error

    Solution:

    list1 contains integers as its elements. Hence these cannot be concatenated to string str1 by simple "+" operand. These should be converted to string first by use of str() function,then only these will get concatenated.


Question: 9 -

Guess the correct output of the following code?

str1 = "PYnative"
print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])

Options:
  1. None of these

  2. PYn PYnat ive PYnativ vitanYP

  3. Yna PYnat tive PYnativ vitanYP

  4. Yna PYnat tive PYnativ PYnativ

  5. Answer:

    Yna PYnat tive PYnativ PYnativ

    Solution:

    We can use a slice operator [] to get a substring.

    Syntaxs[start : end]


Question: 10 -

Which of the following will give "Simon" as output?

If str1="John,Simon,Aryan"

Options:
  1. print(str1[-11:-6])

  2. print(str1[-7:-11])

  3. print(str1[-11:-7])

  4. print(str1[-7:-12])

  5. Answer:

    print(str1[-11:-6])

    Solution:

    Slicing takes place at one index position less than the given second index position of the string. So,second index position will be -7+1=-6.