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
結論
上文描述了所有可以用來將布林值與字串連線的不同方法。根據應用領域和易用性,可以選擇上述任何一種方法。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP