-2
str=" "
while str:
    str=s.readline()
    print(str,end=" ")
    #print(type(str))

I am getting the output correctly by this code but I am not able to understand the functioning of the code specially how the while condition is working in this case.

  • Try `print(bool(" "))` and `print(bool(""))` and see if that helps – JonSG Aug 16 '23 at 16:32
  • Your str is containing single space character which is not empty hence `bool(str)` evaluates to `True`. – novice Aug 16 '23 at 16:32
  • 4
    as an aside... `str` is a bad variable name because it is also the name of Python's builtin `str` function. Overwriting builtins by defining a var of the same name is sometimes called "shadowing" and it makes it impossible to use the builtin in the scope where the new var is defined. – Anentropic Aug 16 '23 at 16:33

0 Answers0