Python 将布尔值连接成字符串的方法
可以参考本文了解不同的方法将布尔值连接成字符串。所有不同的方法都可以在不同的情况下使用。
将布尔值连接成字符串的不同方法
字符串函数
让我们通过一个例子来更好地理解:
示例
# Case 1
value_of_boolean = True # The value of Boolean is given as input to the function
final_string = "The value of the boolean will be displayed as: " + str(value_of_boolean) # The Boolean is converted into string with the help of str function
print(final_string)
# Note: In Boolean, there are only two types of outputs, that is, True & False
# Case 2
value_of_boolean = False # The value of Boolean is given as input to the function
final_string = "The value of the boolean will be displayed as: " + str(value_of_boolean) # The Boolean is converted into string with the help of str function
print(final_string)
输出
上面示例的输出如下:
The value of the boolean will be displayed as: True # Output of case-1
The value of the boolean will be displayed as: False # Output of case-2
格式化字符串字面量
让我们通过一个例子来更好地理解它:
示例
# Case 1
value_of_boolean = True # The value of Boolean is given as input to the function
final_string = f"The value of the boolean will be displayed as: {value_of_boolean}" # The value of Boolean is directly added to the string with the help of curly brackets
print(final_string)
# Note: In Boolean, there are only two types of outputs, that is, True & False
# Case 2
value_of_boolean = False # The value of Boolean is given as input to the function
final_string = f"The value of the boolean will be displayed as: {value_of_boolean}" # The value of Boolean is directly added to the string with the help of curly brackets
print(final_string)
输出
上面示例的输出结果如下:
The value of the boolean will be displayed as: True # Output of case-1
The value of the boolean will be displayed as: False # Output of case-2
过时的方法
我们很少使用这种技术。通过使用%符号,将布尔值与字符串连接在一起的过程中。让我们通过一个例子更好地理解它:
示例
# Case 1
value_of_boolean = True # The value of Boolean is given as input to the function
result = "The value of the boolean will be displayed as: %s" % value_of_boolean # With the help of % symbol the Boolean is linked with the string
print(result)
# Note: In Boolean, there are only two types of outputs, that is, True & False
# Case 2
value_of_boolean = False # The value of Boolean is given as input to the function
final_string = "The value of the boolean will be displayed as: %s" % value_of_boolean # With the help of % symbol the Boolean is linked with the string
print(final_string)
#Note: This is a very old method and might not run in some of the versions of pythons
输出
上述示例的输出将如下所示: –
The value of the boolean will be displayed as: True
# Output of case-1 The value of the boolean will be displayed as: False # Output of case-2
在列表中使用Join函数
当有多个字符串以列表形式存在,并且需要使用布尔运算进行连接时,可以使用这种方法。让我们通过一个例子来更好地理解它:-
示例
# Case 1
value_of_boolean = True # The value of Boolean is given as input to the function
list_of_strings = ["The", "value", "of", "the", "boolean", "will", "be", "displayed", "as:", str(value_of_boolean)] # A list of strings is connected with Boolean with the help of str function
final_string = " ".join(list_of_strings) # All the different strings are combined into one common string
print(final_string)
# Note: In Boolean, there are only two types of outputs, that is, True & False
# Case 2
value_of_boolean = False # The value of Boolean is given as input to the function
list_of_strings = ["The", "value", "of", "the", "boolean", "will", "be", "displayed", "as:", str(value_of_boolean)] # A list of strings is connected with Boolean with the help of str function
final_string = " ".join(list_of_strings) # All the different strings are combined into one common string
print(final_string)
#Note: This method is used only in the case of many different strings
输出
上面例子的输出将如下所示: –
The value of the boolean will be displayed as: True # Output of case-1
The value of the boolean will be displayed as: False # Output of case-2
格式方法
在这种方法中,我们使用format函数将布尔值与字符串连接起来。让我们举一个例子来更好地理解:
示例
# Case 1
value_of_boolean = True # The value of Boolean is given as input to the function
final_string = "The value of the boolean will be displayed as: {}".format
(value_of_boolean) # The Boolean is linked with the string with the help of format function
print(final_string)
# Note: In boolean, there are only two types of outputs, that is, True & False
# Case 2
value_of_boolean = False # The value of Boolean is given as input to the function
final_string = "The value of the boolean will be displayed as: {}".format(value_of_boolean)# The Boolean is linked with the string with the help of format function
print(final_string)
输出
上面示例的输出如下:
The value of the boolean will be displayed as: True # Output of case-1
The value of the boolean will be displayed as: False # Output of case-2
结论
上述文章描述了用于连接布尔值和字符串的各种不同方法。根据应用领域和使用便捷性,可以使用上述任何一种方法。