Convert Python boolean values to strings Yes or No
Last Updated :
22 Jan, 2024
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 instead of True or False.
Convert Python boolean values to String (Yes or No)
Below are some ways by which we can convert Python Boolean values into String in Python:
- Using if-else Statements
- Using Lambda Function
- Using List Comprehension
- Using map() Function
Convert Python boolean values to String Using if-else Statements
In this example, a boolean variable `bool_val` is converted to the string "Yes" if it's `True` and "No" if it's `False` using if-else statements.
Python3
bool_val = True
result = "Yes" if bool_val else "No"
print("Original boolean value:", bool_val)
print("Converted string:", result)
OutputOriginal boolean value: True
Converted string: Yes
Pytho Boolean to Yes or No String Using Lambda Function
In this example, we are using lambda functions to convert a list of strings where "Yes" represents `True` and "No" represents `False`. A lambda function `lambda x: "Yes" if x else "No"` is applied to each element using the `map` function, resulting in the list `string_values`.
Python3
# Sample list of boolean values
bool_values = [True, False, True, True, False]
# Lambda function to convert boolean to "Yes" or "No"
bool_string = lambda x: "Yes" if x else "No"
# Use map to apply the lambda function to the list
string_values = list(map(bool_string, bool_values))
print("Original boolean value:", bool_values)
print("Converted string:", string_values)
OutputOriginal boolean value: [True, False, True, True, False]
Converted string: ['Yes', 'No', 'Yes', 'Yes', 'No']
Boolean to Python String Using List Comprehension
In this example, a list `bool_values` containing boolean elements is converted to a list of strings using a list comprehension. Each boolean element is transformed into "Yes" if it's `True`, and "No" if it's `False`.
Python3
bool_values = [True, False, True, True, False]
result = ["Yes" if x else "No" for x in bool_values]
print("Original boolean value:", bool_values)
print("Converted string:", result)
OutputOriginal boolean value: [True, False, True, True, False]
Converted string: ['Yes', 'No', 'Yes', 'Yes', 'No']
Convert Boolean to Python String Using map() Function
In this example, boolean values are converted into string using map() function. Here, the `map` function and lambda function is applied to each boolean element, converting 'True' to "Yes" and 'False' to "No".
Python3
bool_values = [True, False, True]
result = list(map(lambda x: "Yes" if x else "No", bool_values))
print("Original boolean value:", bool_values)
print("Converted string:", result)
OutputOriginal boolean value: [True, False, True]
Converted string: ['Yes', 'No', 'Yes']
Similar Reads
How to convert string to boolean in PHP? Given a string and the task is to convert given string to its boolean. Use filter_var() function to convert string to boolean value. Examples: Input : $boolStrVar1 = filter_var('true', FILTER_VALIDATE_BOOLEAN); Output : true Input : $boolStrVar5 = filter_var('false', FILTER_VALIDATE_BOOLEAN); Output
2 min read
How to Convert String to Boolean in TypeScript ? In Typescript, sometimes you receive the data as strings but need to work with boolean values or identify the boolean equivalent of it.There are several approaches to convert string to boolean in TypeScript which are as follows:Table of ContentUsing Conditional StatementUsing JSON.parse() MethodUsin
4 min read
How to convert String to Boolean in Ruby? In this article, we will learn how to convert string to Boolean in Ruby using various methods. Letâs understand each of them with the help of examples. Table of Content Converting string to Boolean using String#casecmpConverting string to Boolean using String#downcaseConvert string to Boolean using
3 min read
Convert Boolean To String In Pandas Dataframe Pandas, a powerful data manipulation library in Python, offers multiple ways to convert boolean values to strings within a DataFrame. In this article, we will see how to convert boolean to String in Pandas DataFrame in Python. Python Convert Boolean To String In Pandas DataframeBelow are some exampl
3 min read
Convert Boolean Result into Number/Integer in JavaScript A JavaScript boolean represents one of two values: true or false. However, if one wants to convert a variable that stores a boolean value, into an integer "0" or "1", they can do so using multiple approaches. Below are the approaches to convert a boolean to a number/integer in JavaScript:Table of Co
3 min read
How to parse boolean values with `argparse` in Python Command-line arguments are a powerful feature of many programming languages, including Python. They allow developers to specify options or parameters when running a script, making it more flexible and customizable. However, the process of parsing these arguments can be a tedious and error-prone task
5 min read