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方法

在这种方法中,我们使用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

结论

上文描述了所有可以用来将布尔值与字符串连接的不同方法。根据应用领域和易用性,可以选择上述任何一种方法。

更新于:2023年8月7日

147 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告