Python | Ways to concatenate boolean to string

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



Resultant String :  Facts are True

 
Method #2: Using str

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Resultant String :  Facts are True

 
Method #3: Using %s

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Resultant String :  Facts are True


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.