Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!

PythonPr's tweet image. Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!

Answer: B Solution: This is an application of slicing in python. As you know, [] is used for indexing, ie, picking out one item from a list or string. But there is more to it. [] can also be used for slicing, ie, to extract multiple items. Let's see how it works +


"python"[2:5] creates a substring of "python", starting at index 2, and stopping before 5. The indices are: 0 p 1 y 2 t <-- start index 3 h 4 o 5 n <-- stop index (excluded) Thus, the result is "tho" Hence, the print will output tho and that's the answer


United States 趨勢
Loading...

Something went wrong.


Something went wrong.