Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Did you know that the below code returns True?

>>> bool("False")
True

That's because False is a string and boolean of non-empty string is True. There are workarounds:

1- s == 'True'

Do not use this piece of code if you don't know what you're doing. Misusing this technic puts a mess in your code! Be cautious.

2- The other more elegant way is to use json library like below:

>>> import json
>>> json.loads("false".lower())
False
>>> json.loads("True".lower())
True

NOTE: you need to make it lower case in order to turn the string into a boolean value.

#python #boolean #true #false #cast #convert #bool