EN
Python - convert string to boolean
0 points
In this article, we would like to show you how to convert string to boolean in Python.
In this example, we use bool()
function to convert string and empty_string
to the boolean values.
xxxxxxxxxx
1
string = "abc"
2
empty_string = ""
3
4
bool_value1 = bool(string)
5
bool_value2 = bool(empty_string)
6
7
print(bool_value1) # True
8
print(bool_value2) # False
9
10
print(type(bool_value1)) # <class 'bool'>
11
print(type(bool_value2)) # <class 'bool'>
Output:
xxxxxxxxxx
1
True
2
False
3
<class 'bool'>
4
<class 'bool'>