Skip to content
Related Articles

Related Articles

Python | Ways to concatenate boolean to string
  • Last Updated : 29 Jun, 2019

Given a string and a boolean value, write a Python program to concatenate the string with a boolean value, given below are few methods to solve the task.

Method #1: Using format()




# Python code to demonstrate 
# to concatenate boolean value
# with string
  
# Initialising string and boolean value
ini_string = "Facts are"
value = True
  
# Concatenate using format
res = str(ini_string+" {}").format(value)
  
# Printing resultant string
print ("Resultant String : ", res)
Output:
Resultant String :  Facts are True

 
Method #2: Using str




# Python code to demonstrate 
# to concatenate boolean value
# with string
  
# 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

 
Method #3: Using %s




# Python code to demonstrate 
# to concatenate boolean value
# with string
  
# Concatenate using % s
answer = True
res = "Facts are %s" %answer
  
# Printing resultant string
print ("Resultant String : ", res)
  
         
Output:
Resultant String :  Facts are True

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :