What does this print?>>> a = "Python is cool">>> print(a[7:-5])
Question
What does this print?>>> a = "Python is cool">>> print(a[7:-5])
Solution
This will print "is".
Here's the step by step explanation:
-
In Python, strings are indexed with the first character having index 0. Negative indexing starts from the end of the string.
-
The string "Python is cool" is indexed as follows:
P(0) y(1) t(2) h(3) o(4) n(5) (6) i(7) s(8) (9) c(10) o(11) o(12) l(13)
-
a[7:-5] means start at index 7 and end at 5 places from the end (or -5 when counted from the end).
-
So, it starts at 'i' (index 7) and ends at ' ' (space after 'is', which is index -5 when counted from the end).
-
Therefore, it prints "is".
Similar Questions
What does this command line print?>>> a = "Python is cool">>> print(a[-2])
What does this command line print?>>> a = "Python is cool">>> print(a[:6])PythoPythonPython isis cool
What is the output of the following code? word = "Python" print(word[::-1])
What is printed by the following code snippet?numList=[1,3,6,9]for i in range(len(numList)):print (i, end="-")print()A. 1-3-6-9-B. 1-3-6-9C. 0-1-2-3D. 0-1-2-3-E. None of the above
What will be the output of the following Python code?def ct(y): global y y+=2 print(y)ct(5)print("hi")Optionserror77hihi
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.