Q. 1

Which is correct to print the following ?
Welcome-to-python-easy

Options:

Explanation

Correct answer is : C

"welcome".title() returns "Welcome" and "-" will concatenate the strings

Discuss It

Q. 2

Which of the following results in a SyntaxError ?

Options:

Explanation

Correct answer is : C

Python strings can be declared with pair of " (double quote) or ' (single quote)
You can keep double quote inside pair of single quote or the other way round.(like option A and B)
If you want to keep double quote inside double quote then you need to keep \ backslash to escape it (like option D)

Discuss It

Q. 3

What is the output of the following program ? 

k = bytearray(b'python')
k.extend(b'easy')
print(k)

 

Options:

Explanation

Correct answer is : C

Both the bytes will be combined to provide  bytearray(b'pythoneasy')

Discuss It

Q. 4

What is the output of the following code ?

"".join(["2", 10])

 

Options:

Explanation

Correct answer is : D

A TypeError will be raised if there are any non-string values in iterable in join.

!0 is an integer

>>> "".join(["2", 10])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 1: expected str instance, int found
>>>

 

Discuss It

Q. 5

What is the output of the following code ?

>>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
>>> comment_string.strip('.#! ')

 

Options:

Explanation

Correct answer is : C

The outermost leading and trailing chars argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars. A similar action takes place on the trailing end

Refer to - https://docs.python.org/3/library/stdtypes.html#str.strip

Discuss It

Q. 6

What is the output of the following code ?

>>> s = "they're bill's friends from the UK"
>>> s.title()

 

Options:

Explanation

Discuss It

Q. 7

What is the output of the following code ? 

>>> s = "they're bill's friends from the UK"
>>> s.capitalize()

 

Options:

Explanation

Correct answer is : D

str.capitalize() Return a copy of the string with its first character capitalized and the rest lowercased

Refer to - https://www.pythoneasy.com/python-programming-tutorial/python-string-methods

Discuss It

Q. 8

What is the output of the following code ?

>>> prefix_strings = 'they', 'he'
>>> s = "they're bill's friends from the UK"
>>> p = "he is bill's friends from the UK"
>>> s.startswith(prefix_strings), p.startswith(prefix_strings)

 

Options:

Explanation

Correct answer is : A

str.startswith(prefix[, start[, end]])  Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for

Discuss It

Q. 9

What is the output of the following code ?

>>> s = "they're bill's friends from the UK"
>>> s.endswith('from', 5, 27)

 

Options:

Explanation

Correct answer is : B

"from" string available at index 23 and 27.

str.endswith(suffix[, start[, end]]) - With optional start, test beginning at that position. With optional end, stop comparing at that position.

so 'from' stops at 27 , so it will match endswith

Discuss It

Q. 10

What is the output of the following code ? 

>>> p = 'he is from US'
>>> e = 'he is from UK'
>>> suffix_strings = 'UK', 'IND'
>>> e.endswith(suffix_strings), p.endswith(suffix_strings)

 

Options:

Explanation

Correct answer is : B

str.endswith(suffix[, start[, end]]) Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for

"e" string ends with "UK" and suffix_strings contain " UK" and "IND". So  e.endswith(suffix_strings) prints True

"p" string does not end with any of " UK" and "IND". so p.endswith(suffix_strings) prints false

Discuss It
Submit Your Answers

Interview Questions

on
Data Types

Tutorial

on
Data Types

Programs

on
Data Types
Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description