OneBite.Dev - Coding blog in a bite size

convert variable from string to boolean in python

Code snippet on how to convert variable from string to boolean in python

  variable = 'False'
  variable = variable.lower()
  variable = (variable == 'true')

This code converts the variable from string to boolean. The first line assigns the string “False” to a variable. The second line converts the upper case letter in the string to a lower case one. The last line checks if the variable is equal to the string “true”. If it is, then the variable is assigned the boolean value of true. If not, then the variable is assigned the boolean value of false. In either case, this changes the variable’s type from string to boolean.

python