Python|将布尔值连接到字符串的方法
给定一个字符串和一个布尔值,编写一个Python程序将字符串与一个布尔值连接起来,下面给出了一些解决该任务的方法。
方法 #1:使用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)
输出:
Resultant String : Facts are True
方法 #2:使用 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)
输出:
Resultant String : Facts are True
方法 #3:使用 %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)
输出:
Resultant String : Facts are True