Ways to Concatenate Boolean to String in Python


One can refer this article to learn about different methods to concatenate Boolean to string. All the different methods can be used in different cases.

Different Methods to Concatenate Boolean to String

String Function

Let's take an example to understand it in a proper manner: -

Example

# Case 1
value_of_boolean = True # The value of Boolean is given as input to the function
final_string = "The value of the boolean will be displayed as: " + str(value_of_boolean) # The Boolean is converted into string with the help of str function
print(final_string) 

# Note: In Boolean, there are only two types of outputs, that is, True & False

# Case 2
value_of_boolean = False # The value of Boolean is given as input to the function
final_string = "The value of the boolean will be displayed as: " + str(value_of_boolean) # The Boolean is converted into string with the help of str function
print(final_string) 

Output

The output of the above example will be as follows: -

The value of the boolean will be displayed as: True # Output of case-1
The value of the boolean will be displayed as: False # Output of case-2

Formatted String Literals

Let's take an example to understand it in a proper manner: -

Example

# Case 1
value_of_boolean = True # The value of Boolean is given as input to the function
final_string = f"The value of the boolean will be displayed as: {value_of_boolean}" # The value of Boolean is directly added to the string with the help of curly brackets
print(final_string) 

# Note: In Boolean, there are only two types of outputs, that is, True & False

# Case 2
value_of_boolean = False # The value of Boolean is given as input to the function
final_string = f"The value of the boolean will be displayed as: {value_of_boolean}" # The value of Boolean is directly added to the string with the help of curly brackets
print(final_string) 

Output

The output of the above example will be as follows: -

The value of the boolean will be displayed as: True # Output of case-1
The value of the boolean will be displayed as: False # Output of case-2

Deprecated Method

Only very rarely do we utilise this technique. With the help of the % symbol, the Boolean is connected to the string in this procedure. Let's take an example to understand it in a better way: -

Example

# Case 1
value_of_boolean = True # The value of Boolean is given as input to the function
result = "The value of the boolean will be displayed as: %s" % value_of_boolean # With the help of % symbol the Boolean is linked with the string
print(result) 

# Note: In Boolean, there are only two types of outputs, that is, True & False

# Case 2
value_of_boolean = False # The value of Boolean is given as input to the function
final_string = "The value of the boolean will be displayed as: %s" % value_of_boolean # With the help of % symbol the Boolean is linked with the string
print(final_string) 

#Note: This is a very old method and might not run in some of the versions of pythons

Output

The output of the above example will be as follows: - 

The value of the boolean will be displayed as: True 
# Output of case-1 The value of the boolean will be displayed as: False # Output of case-2

Join Function in a List

When there are several strings in the form of a list and those strings need to be connected with a Boolean, this approach is utilised. Let's take an example to understand it in a better way: -

Example

# Case 1
value_of_boolean = True # The value of Boolean is given as input to the function
list_of_strings = ["The", "value", "of", "the", "boolean", "will", "be", "displayed", "as:", str(value_of_boolean)] # A list of strings is connected with Boolean with the help of str function
final_string = " ".join(list_of_strings) # All the different strings are combined into one common string
print(final_string) 

# Note: In Boolean, there are only two types of outputs, that is, True & False

# Case 2
value_of_boolean = False # The value of Boolean is given as input to the function
list_of_strings = ["The", "value", "of", "the", "boolean", "will", "be", "displayed", "as:", str(value_of_boolean)] # A list of strings is connected with Boolean with the help of str function
final_string = " ".join(list_of_strings) # All the different strings are combined into one common string
print(final_string) 

#Note: This method is used only in the case of many different strings

Output

The output of the above example will be as follows: -

The value of the boolean will be displayed as: True # Output of case-1
The value of the boolean will be displayed as: False # Output of case-2

Format Method

In this method we concatenate the Boolean with the strings with the help of format function. Let's take an example to understand it in a better way:

Example

# Case 1
value_of_boolean = True # The value of Boolean is given as input to the function
final_string = "The value of the boolean will be displayed as: {}".format
(value_of_boolean) # The Boolean is linked with the string with the help of format function 
print(final_string) 

# Note: In boolean, there are only two types of outputs, that is, True & False

# Case 2
value_of_boolean = False # The value of Boolean is given as input to the function
final_string = "The value of the boolean will be displayed as: {}".format(value_of_boolean)# The Boolean is linked with the string with the help of format function 
print(final_string) 

Output

The output of the above example will be as follows: -

The value of the boolean will be displayed as: True # Output of case-1
The value of the boolean will be displayed as: False # Output of case-2

Conclusion

The above article describes all the different ways that can be used to concatenate a Boolean with a string. One can use any of the above-mentioned method as per convenience in the field of application along with the ease of use.

Updated on: 07-Aug-2023

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements