Converting from a string to boolean in Python

preview_player
Показать описание
PLEASE COMMENT, SUBSCRIBE, AND SHARE!!!
I know I have personally run into this problem many times when I first started learning python, mainly due to forgetting what builtin functions did what. Thankfully, stackoverflow

This is what I wrote for this video:

# Todo:
# 1. Create a string variable that is True or False
# and convert it to a boolean data type

stringvariable = 'True'
print bool(stringvariable)# 1.1 This will print
# out the boolean version of stringvariable

# 2. Now we will test to see if the variable has truly
# changed from a string to a boolean:
if (bool(stringvariable) is True):
print 'Please comment and subscribe!'

Рекомендации по теме
Комментарии
Автор

its not correct, bool will always make the value True ( bool ("False")= True). Wasted 30 minutes because of you

Idoking
Автор

It didnt work, simply, bool("") == False and bool("whatever youi want, cause it's str) == True, to be false, str must be empty

Choquette-sucrée
visit shbcf.ru