Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Ways to concatenate boolean to string

Python | Ways to concatenate boolean to string

In Python, concatenating Booleans with strings is a common task when you want to include the truth value of a Boolean variable in a text message or output. Booleans in Python are represented by the values True and False. Given a string and a Boolean value, write a Python program to concatenate the string with a Boolean value.

Example:

Input: ini_string="Facts are"(A boolean variable representing a truth value)
Output: True(A concatenated string that includes the Boolean value)
Explanation: In this, we concatenate Boolean with strings in Python.

Python Ways to Boolean to String

Here we can check different methods for converting Python Boolean to String.

Convert Python Ways to Boolean to String using Format() 

In Python, you can convert Boolean values to strings using the str.format() method, which provides a flexible way to format strings by replacing placeholders {} with the corresponding values. Use the format string placeholders and send the boolean variables as parameters to the str.format() function to convert boolean values to strings. The resulting formatted string will automatically translate the Boolean values into their string equivalents.

Python3




# Initialising string and boolean value 
ini_string = "Facts are"
value = True
  
# Concatenate using format 
res = str(ini_string+" {}").format(value) 
  
print ("Resultant String : ", res)


Output

Resultant String :  Facts are True

Convert Python Ways to Boolean to String using Str 

In Python, you can convert Boolean values to strings using the built-in str() function. Any Python object, including boolean values, can be converted to a string representation using the str() function. A Boolean value will automatically be translated into the string “True” or “False” when you send it as an argument to the str() method, depending on how true it is.

Python3




# Initialising string and boolean value 
ini_string = "Facts are"
value = True
  
# Concatenate using str 
res = ini_string +" "+str(value) 
  
# Printing resultant string 
print ("Resultant String : ", res)


Output

Resultant String :  Facts are True

Convert Bool to a String using % Formatting

In Python, you can convert Boolean values to strings using the % formatting operator. Using placeholders %s or %r, you can put values into a string to represent them as a regular or “raw” string, respectively, using the % operator.

Python3




# Concatenate using % s 
answer = True
res = "Facts are %s" %answer 
  
# Printing resultant string 
print ("Resultant String : ", res) 


Output

Resultant String :  Facts are True

Convert Python Ways to Boolean to String using F-strings

In Python, you can convert Boolean values to strings using f-strings, a more modern and preferred method for string formatting introduced in Python 3.6.Without the use of placeholders or format specifiers, F-strings offer a clear and understandable way to embed expressions and variables, including Boolean values, directly within a string.

Python3




  # Initialising string and boolean value
ini_string = "Facts are"
value = True
  
# Concatenate using f-strings
res = f"{ini_string} {value}"
  
print ("Resultant String : ", res)


Output

Resultant String :  Facts are True

Time complexity: O(1).
Auxiliary space: O(n).

Convert bool to String in Python using Join() Method

In Python, you can convert Boolean values to strings using the join() method, which is a powerful and versatile way to concatenate elements of an Iterable into a single string. Python’s join() method is used to concatenate the elements of the string list into a single string, with an empty string as the separator

Python3




b = True
s = "".join(["This statement is ", str(b)])
print(s)


Output

This statement is True

RELATED ARTICLES

Most Popular

Recent Comments