Python - Convert String Truth values to Boolean Last Updated : 10 Jan, 2025 Summarize Suggest changes Share Like Article Like Report Converting string truth values like "True" and "False" into their corresponding boolean values in Python is a straightforward yet useful operation. Methods such as dictionaries, direct comparison, and built-in functions offer simple and efficient ways to achieve this transformation for various use cases.Using eval() Functioneval() function in Python is used to evaluate a string as a Python expression and convert it into the corresponding object Python s = "True" # Convert string to boolean using eval boolean_value = eval(s) print(boolean_value) OutputTrue Explanation :eval(s) function evaluates the string s as a Python expression, converting it into its corresponding Python object, in this case, the boolean value True.The result, stored in boolean_value, is then printed, showing the evaluated boolean value True. Using str.lower() with ComparisonUsing str.lower() with comparison allows you to convert a string to lowercase before performing a case-insensitive comparison. This ensures that the comparison ignores differences in letter casing between the two strings. Python s = "True" # Convert string to boolean by comparing lowercase value boolean_value = s.strip().lower() == "true" print(boolean_value) OutputTrue Explanation :s.strip().lower() method first removes any leading or trailing spaces using strip(), then converts the string to lowercase using lower(). This ensures the comparison is case-insensitive.The string is then compared to "true", and if they match, the result is True; otherwise, it is False. The result is stored in boolean_value and printed.Using a Dictionary for LookupUsing a dictionary for lookup allows you to map specific string values to corresponding boolean values or other types. This provides an efficient way to perform case-insensitive or custom comparisons by leveraging the dictionary's key-value pairs. Python s = "False" # Dictionary to map string values to boolean bool_map = {"true": True, "false": False} # Convert string to boolean using the dictionary boolean_value = bool_map.get(s.strip().lower(), False) print(boolean_value) OutputFalse Explanation :The bool_map dictionary maps the lowercase string values "true" and "false" to their corresponding boolean values True and False.The s.strip().lower() converts the input string to lowercase and removes any extra spaces, then bool_map.get() retrieves the corresponding boolean value, defaulting to False if the string doesn't match. The result is stored in boolean_value and printed. Advertise with us Next Article Ways to concatenate boolean to string - Python M manjeet_04 Follow Similar Reads Convert Python boolean values to strings Yes or No When you're using true or false values in Python, changing them to "Yes" or "No" strings can make your code easier to read. This article shows different ways to do it, giving you choices based on how you like to write your code. Here, we will convert the Python Boolean values to string as Yes or No 3 min read Ways to concatenate boolean to string - Python Concatenating a boolean to a string in Python involves converting the boolean value (True or False) into a string format and then combining it with other string elements. For example, given s = "Facts are" and v = True, the expected output after concatenation is "Facts are True". Let's explore diffe 2 min read Python | Ways to convert Boolean values to integer Given a boolean value(s), write a Python program to convert them into an integer value or list respectively. Given below are a few methods to solve the above task. Convert Boolean values to integers using int()Â Converting bool to an integer using Python typecasting. Python3 # Initialising Values bo 3 min read How to Convert Bool (True/False) to a String in Python? In this article, we will explore various methods for converting Boolean values (True/False) to strings in Python. Understanding how to convert between data types is crucial in programming, and Python provides several commonly used techniques for this specific conversion. Properly handling Boolean-to 2 min read Convert Dictionary values to Strings In Python, dictionaries store key-value pairs, where values can be of any data type. Sometimes, we need to convert all the values of a dictionary into strings. For example, given the dictionary {'a': 1, 'b': 2.5, 'c': True}, we might want to convert the values 1, 2.5, and True to their string repres 3 min read Python - Test Boolean Value of Dictionary Sometimes, while working with data, we have a problem in which we need to accept or reject a dictionary on the basis of its true value, i.e all the keys are Boolean true or not. This kind of problem has possible applications in data preprocessing domains. Let's discuss certain ways in which this tas 9 min read Article Tags : Python Python Programs Python list-programs Python string-programs Practice Tags : python Like