Open In App

Python | Ways to concatenate boolean to string

Last Updated : 08 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

Python | Ways to concatenate tuples
Many times, while working with records, we can have a problem in which we need to add two records and store them together. This requires concatenation. As tuples are immutable, this task becomes little complex. Let's discuss certain ways in which this task can be performed. Method #1 : Using + operator This is the most Pythonic and recommended meth
6 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. C/C++ Code # Initialising Values bool_val = True # Printing initial Values print(
3 min read
Python | Pandas Series.str.cat() to concatenate string
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas str.cat() is used to concatenate strings to the passed caller series of string. Distinct values from a different series can be pas
3 min read
Python | Concatenate N consecutive elements in String list
Sometimes, while working with data, we can have a problem in which we need to perform the concatenation of N consecutive Strings in a list of Strings. This can have many applications across domains. Let's discuss certain ways in which this task can be performed. Method #1: Using format() + zip() + iter() + list comprehensionThe combination of the a
8 min read
Python - Concatenate Dictionary string values
Sometimes, while working with dictionaries, we might have utility problem in which we need to perform elementary operation among the common keys of dictionaries. This can be extended to any operation to be performed. Let’s discuss string concatenation of like key values and ways to solve it in this article. Method #1 : Using dictionary comprehensio
5 min read
Python - Concatenate string rows in Matrix
The problems concerning matrix are quite common in both competitive programming and Data Science domain. One such problem that we might face is of finding the concatenation of rows of matrix in uneven sized matrix. Let’s discuss certain ways in which this problem can be solved. Method #1 : Using join() + list comprehension The combination of above
3 min read
Python - Concatenate String values in Dictionary List
Sometimes, while working with Python records data, we can have a problem in which we require to perform concatenation of string values of keys by matching at particular key like ID. This kind of problem can have application in web development domain. Let's discuss certain way in which this task can be performed. Input : test_list = [{'id': 17, 'gfg
4 min read
Python - Concatenate Ranged Values in String list
Given list of strings, perform concatenation of ranged values from the Strings list. Input : test_list = ["abGFGcs", "cdforef", "asalloi"], i, j = 3, 5 Output : FGorll Explanation : All string sliced, FG, or and ll from all three strings and concatenated. Input : test_list = ["aGFGcs", "cforef", "aalloi"], i, j = 1, 4 Output : GFGforall Explanation
5 min read
Python - Concatenate Random characters in String List
Given a String list, perform concatenation of random characters. Input : test_list = ["Gfg", "is", "Best", "for", "Geeks"] Output : "GiBfe" Explanation : Random elements selected, e.g G from Gfg, etc.Input : test_list = ["Gfg", "is", "Best"] Output : "fst" Explanation : Random elements selected, e.g t from Best, etc. Method #1 : Using loop + random
6 min read
Python program to Concatenate Kth index words of String
Given a string with words, concatenate the Kth index of each word. Input : test_str = 'geeksforgeeks best geeks', K = 3 Output : ktk Explanation : 3rd index of "geeksforgeeks" is k, "best" has 't' as 3rd element. Input : test_str = 'geeksforgeeks best geeks', K = 0 Output : gbg Method #1 : Using join() + list comprehension + split() In this, we per
4 min read
Practice Tags :