
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to check if type of a variable is string in Python?
In this article, we are going to find out how to check if the type of a variable is a string in Python.
The first approach is by using the isinstance() method. This method takes 2 parameters, the first parameter being the string that we want to test and the next parameter is the keyword str. This method will return True if the given input is a string, otherwise, it returns False.
It specifies that when we pass an object and class, or tuple of classes, to the isinstance() method, it will return True if the object's data type matches the class provided and False otherwise.
Example 1
In the example given below, we are taking input and checking whether it is a string or not using the isinstance() method and printing if the input is a string or not −
str1 = "Tutorialspoint" print("The given string is") print(str1) print("Checking if the given input is string or not") print(isinstance(str1, str))
Output
The output of the above example is as shown below −
The given string is Tutorialspoint Checking if the given input is string or not True
Example 2
In the example given below, we are taking the same program as above but with different input and checking the type of the input and printing if the input is a string or not.
str1 = 10 print("The given string is") print(str1) print("Checking if the given input is string or not") print(isinstance(str1, str))
Output
The output of the above example is given below −
The given string is 10 Checking if the given input is string or not False
Using type() method
The second approach is by using the inbuilt method type(). This method takes an input and returns the type of the given input. We will return True if the type is string otherwise return False.
Example 1
In the example given below, we are taking an input and checking if the given input is string or not using type() method and printing if the input is a string or not −
str1 = "Tutorialspoint" print("The given string is") print(str1) print("Checking if the given input is a string or not") print(type(str1) == str)
Output
The output of the above example is as shown below −
The given string is Tutorialspoint Checking if the given input is a string or not True
Example 2
In the example given below, we are taking the same program as above but we are taking a different input and we are checking if it belongs to string or not −
str1 = 10 print("The given string is") print(str1) print("Checking if the given input is string or not") print(type(str1) == str)
Output
The output of the above example is given below −
The given string is 10 Checking if the given input is string or not False
- Related Articles
- Python - Check if a variable is string
- How can I check if a JavaScript variable is function type?
- How to check if String value is Boolean type in java?
- How to check if a string is alphanumeric in Python?
- Check if variable is tuple in Python
- How to check if a string in Python is in ASCII?
- How to check if a variable is NaN in JavaScript?
- How to check if a variable is boolean in JavaScript?
- How to check if a string is a valid keyword in Python?
- How to check the type of a variable or object in JavaScript?
- How to check if a string contains only one type of character in R?
- How do I check if a Python variable exists?
- How to check if a variable is an integer in JavaScript?
- How to check if a variable is an array in JavaScript?
- How to check if a variable is NULL in C/C++?
