Open In App

Convert Python boolean values to strings Yes or No

Last Updated : 22 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Using if-else Statements
  2. Using Lambda Function
  3. Using List Comprehension
  4. 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)


Output

Original 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)


Output

Original 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)


Output

Original 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)


Output

Original boolean value: [True, False, True]
Converted string: ['Yes', 'No', 'Yes']




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads