ExamVeda
Login
Home
1
Which of the following is the correct way to create a string in Python?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
2
What is the output of the following code:
word = "Python"
print(word[2])
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
3
What does the len() function do when applied to a string?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
4
Which character is used to access individual elements of a string by index in Python?
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
5
What will be the output of the following code:
string = "Python"
print(string[1:4])
Discuss
Answer & Solution
Answer: Option A
Solution:
Understanding String Slicing in Python

In Python, strings are sequences of characters. We can access parts of a string using slicing.

String slicing uses square brackets `[]` and a colon `:` to specify a range of characters. The format is `string[start:end]`.

The `start` index indicates where to begin (inclusive), and the `end` index indicates where to stop (exclusive).

Let's break down the code:

string = "Python"
This line assigns the string "Python" to the variable string.

print(string[1:4])
This line uses slicing to extract a portion of the string. Let's analyze the indices:

-1 (start index): This means we start at the second character (remember, Python uses zero-based indexing, so the first character is at index 0).
-4 (end index): This means we stop just before the fourth character.

Therefore, string[1:4] will extract characters from index 1 ( 'y' ) up to, but not including, index 4.

The Result:
The characters extracted are "yth".

Therefore, the correct answer is Option A: yth
6
What is the purpose of the str() function in Python?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
7
Which of the following methods is used to find the index of the first occurrence of a substring in a string?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
8
What is the output of the code:
string = "Python"
print(string[-2])
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
9
In Python, which escape sequence is used to represent a newline character?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
10
What is the output of the following code:
string = "Python"
print(string.capitalize())
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board