Which of the following would give "Harry" as output?Given str1="Mary,Harry,John,Sam"str1[4:9]str1[5:10]str1[4:10]str1[5:9]
Question
Which of the following would give "Harry" as output?
Given
str1="Mary,Harry,John,Sam"
str1[4:9]
str1[5:10]
str1[4:10]
str1[5:9]
Solution
The correct answer is str1[5:10]. Here's why:
In Python, string slicing is done in the format str[start:stop], where the start index is inclusive and the stop index is exclusive. The first character of a string is at index 0.
Given str1="Mary,Harry,John,Sam"
- str1[4:9] would give ",Harr" because it starts at index 4 (which is the comma after "Mary") and stops at index 9 (which is the "r" in "Harry").
- str1[5:10] would give "Harry" because it starts at index 5 (which is the "H" in "Harry") and stops at index 10 (which is the comma after "Harry").
- str1[4:10] would give ",Harry" because it starts at index 4 (which is the comma after "Mary") and stops at index 10 (which is the comma after "Harry").
- str1[5:9] would give "Harr" because it starts at index 5 (which is the "H" in "Harry") and stops at index 9 (which is the "r" in "Harry").
Similar Questions
Which of the following will give "Simon" as output?If str1="John,Simon,Aryan"print(str1[-7:-12])print(str1[-11:-7])print(str1[-11:-6])print(str1[-7:-11])
ct answerWhat will be the output of the following Python code?print("Hola {0[0]} and {0[1]}".format(('kris', 'jon')))
2. What will be the output of the following Python code?>>>str="hello">>>str[:2]>>>
2. What is the output when we execute list(“hello”)?a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]b) [‘hello’]c) [‘llo’]d) [‘olleh’]
What will be the output for the following print statements?print(1,2,3,4,sep="*")print(1,2,3,4,sep="#",end="@")
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.