Python Programming - Variables - Discussion
Discussion Forum : Variables - General Questions (Q.No. 32)
32.
What is the output of the following code:
my_string = "hello world"
print(my_string[1:8:2])
Answer: Option
Explanation:
In this code, a string my_string
is defined with the value "hello world"
.
The second line uses slice notation to select every second character from the substring starting at index 1 and ending at index 8.
The expression my_string[1:8:2]
selects the characters with the following indices: 1, 3, 5, and 7, which correspond to "el o"
in the string "hello world"
.
The resulting substring is "el o"
, which is then printed to the console.
Discussion:
Be the first person to comment on this question !
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers