Not able to understand the following question

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

How is the output of this
t
t

??

Hey @Ak07,

while i in iterable syntax basically runs the loop until the provided value i is present in the iterable. Hence in your case, on each iteration the loop checks if the value i is present inside iterable x or not.
One 1st iteration, as value of x="xyztv", the i("t") is present in x, so it executes the body of the loop which basically truncates the value of x by removing the last element. Now if you do the subsequent iteration in the same manner, you will find out that after two iterations there is no “t” in x.

Hence the output you get the output:

t
t

As you are printing t.