- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PythonServer Side ProgrammingProgramming
';
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 isTutorialspointChecking if the given input is string or notTrue
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 = 10print("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 is10Checking if the given input is string or notFalse
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 isTutorialspointChecking if the given input is a string or notTrue
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 = 10print("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 is10Checking if the given input is string or notFalse
Tarun Chandra
Updated on: 07-Dec-2022
28K+ Views
- Related Articles
- How can I check if a JavaScript variable is function type?
- How to check if String value is Boolean type in java?
- Check if variable is tuple in Python
- How to check if a string is alphanumeric 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 NULL in C/C++?
- How to check if a variable is an array in JavaScript?
- How to check if a variable is an integer in JavaScript?
- Check if a string is Colindrome in Python
Kickstart Your Career
Get certified by completing the course
Get Started
Advertisem*nts
';