FLOW OF CONTROL

x = “xyztv”
i = “t”
while i in x:
x = x[:-1]
print(i)

i dont understand output of this

Hi @amangupta
Output of the following code is :
image
Explaination

Note : x[:-1] will retrieve all characters except the last and return a new (sub)string.

while loop will run until the given condtion is true i.e string i is in string x, for the 1st iter ; (‘t’, is in x i.e ‘xyztv’ ) thus it proceed inside the loop, once in loop string x is updates to ‘xyzt’, and it prints i , for 2nd iter ; (‘t’, is in x i.e ‘xyzt’ ) thus it proceed inside the loop, once in loop string x is updates to ‘xyz’, and it prints i , for 3rd iter ; (‘t’, is not in x i.e ‘xyz’ ) thus it won’t proceed inside the loop.

Hope this might helps :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.