Convert boolean to lowercase string in Python

avatar

Borislav Hadzhiev

Last updated: Aug 28, 2022

banner

Photo from Unsplash

Convert boolean to lowercase string in Python #

To convert a boolean value to a lowercase string:

  1. Use the str() class to convert the boolean to a string.
  2. Use the str.lower() method to convert the string to lowercase.
  3. The str.lower() method will return a lowercase string containing the value.
main.py
import json # ✅ convert boolean value to lowercase string my_bool = True my_str = str(my_bool).lower() print(my_str) # 👉️ true # ----------------------------------------- # ✅ check if value is boolean before converting to lowercase string my_str = str(my_bool).lower() if isinstance(my_bool, bool) else my_bool print(my_str) # 👉️ 'true' # ----------------------------------------- # ✅ converting boolean values to JSON my_dict = {'is_subscribed': True} json_str = json.dumps(my_dict) print(json_str) # 👉️ '{"is_subscribed": true}'

We used the str() class to convert the boolean to a string.

main.py
my_bool = True my_str = str(my_bool).lower() print(my_str) # 👉️ true
The last step is to use the str.lower() method to convert the string to lowercase.

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

If you need to convert the string back to a boolean, use the ternary operator.

main.py
my_bool = True my_str = str(my_bool).lower() print(my_str) # 👉️ true # 👇️ convert string back to boolean bool_again = True if my_str.lower() == 'true' else False print(bool_again) # 👉️ True
We check if the lowercase version of the string is equal to True and if it is, we set the value to True, otherwise, we set it to False.

If you only need to convert the value to a lowercase string if it is boolean, use the isinstance method.

main.py
my_bool = True my_str = str(my_bool).lower() if isinstance(my_bool, bool) else my_bool print(my_str) # 👉️ 'true'

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

If the value is a boolean, the expression returns the lowercased, string version of it, otherwise the value is returned as is.

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
my_str = 'is subscribed:' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ True my_bool = True print(type(my_bool)) # 👉️ <class 'bool'> print(isinstance(my_bool, bool)) # 👉️ True

The type class returns the type of an object.

If you need to convert a value to a JSON string, use the json.dumps() method.

main.py
import json my_dict = {'is_subscribed': True} json_str = json.dumps(my_dict) print(json_str) # 👉️ '{"is_subscribed": true}' print(json.dumps(True)) # 👉️ 'true'

JSON booleans are lowercased, so converting the value to a JSON string automatically converts it to lowercase.

The json.dumps method converts a Python object to a JSON formatted string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.