This article discusses a brief introduction of string and boolean datatypes. Lastly, we’ll discuss How to convert a String to Boolean in python. The string is derived datatype used to represent a sequence of characters. They are usually declared using quotation marks. You can use both single quotes or double quotes to declare a string.
string ="Hello World"
Strings in Python are not changeable. Once created, they can’t be changed like tuples in python. Strings are generally used to label information on the graphical user interfaces or many other places like that.
On the other hand, Boolean is a built-in data type in python which return answers in logic types 0/1, Yes/No or true/False.
Ways to Convert String to Boolean in Python
The most important question is what is the purpose of boolean datatypes? Why do we need to convert a string to boolean. Boolean datatypes can be used to check if the input taken from the user is an empty string or non-empty. They can also be used to check if an input is an instance of a particular datatype or not
There can be several other uses also. We’ll discuss different methods along with their uses to convert a string to boolean expression.
Method 1: Conversion of string to boolean using bool() method
Python provides a builtin bool() function which takes string as an input. If the string is empty, the function returns false. For non-empty strings, it return true.
string = input("Enter a string: ") value = bool(string) if(value): print("The entered value is a string") else: print("The string entered is empty")
Enter a string: Welcome to Entechin The entered value is a string
Now press enter without typing anything, and the code will display following output on the screen.
The string entered is empty.
Method 2: Conversion of string to boolean using strtobool() method
distutils.util module provides
strtobool() method which can be used to convert a string to a boolean value i.e., 0 or 1.The method only accepts true, false, t, f yes, no, y, n, off values. Any other value except them raises a ValueError. It converts the positive values to 1 such as true, yes etc. and converts negative values such as no, false etc.
The following code demonstrates the use of strtobool() method to convert a string to boolean.
import distutils.util as module input_value = module.strtobool("True") print("Output: ",input_value)
input_value = module.strtobool("False") print("Output: ",input_value)
input_value = module.strtobool("Hi") print("Output: ",input_value)
ValueError: invalid truth value 'hi'
Method 3: Convert a string to boolean to check if an object is an instance of a particular type
Suppose you’re designing an application where you need an integer. To check whether the input entered by user is an integer or not, you can use boolean datatypes. If you want to know whether an object is an instance of a particular type, you can use the built-in Python function isinstance().
x = int(input("Enter a value: ")) print(isinstance(x,int))
Enter a value: 5 True
In the above example, the data type of input is an integer. Therefore, the isinstance() returns True, but if the data type is not an integer, it returns False. Similarly, you can also check whether the input entered by the user is a string.
x = "Hello" print(isinstance(x,int))
In the example above, the “Hello” is a string but isintance() method checks whether the value stored is an integer or not. Therefore, the output is false. Now lets see what happen if we replace int with str.
In this article, a discussion of converting a string to a boolean in Python is presented. There are different methods to do it. Two of them are discussed in detail along with examples. Let us know about your queries in the comments. If you want to learn more about Python Programming, visit Python Programming Articles.