How to find out if a Python object is a string?


We know that Python is an object oriented programming language. By using the oops concept we can make the code and functions work together in a better way.

The OOPs in python help us to deal with the real time entities with inheritance, polymorphism, encapsulation etc.

Group of objects is called a class. The class contains the blueprint, which takes as the reference to create the object. This contains attributes and methods in a logical entity.

  • Let’s understand the usage of the class in object oriented programming language by a real time scenario.

  • Consider a library. In a library we will have different number of books. Now we want to track each and every book from the library.

  • For a book we will have different attributes like book name, specialization etc. Let’s say the list holds the details of the book. The first element will be the book name and the second element will be the specialization of the book.

  • So now there are some 1000 of books in the library, now we cannot analyze which element is related to which book.

  • And it is difficult as we want to add a new element to a particular book. So in these cases we will go for the classes, to have the better organization.

We also know that there are instances in our python object. In that we will use different data types. If we want to check the datatype of the instance whether string or not we have different methods. Let’s see them one by one.

Using the isinstance() method

One of the methods to check whether the instance is string or not is the instance method. The following syntax can be used for checking if the instance is string or not.

isinstance(obj, basestring)

Where,

  • isinstance is the method

  • obj is the object

  • basestring is the type of the instance

Example

Let’s see an example to check whether the given instance is a string or not. The following code can be used for checking if the instance is string or not.

tstring = "python"
print("The original string : " + str(tstring))
# using isinstance()
# Check if variable is string 
res = isinstance(tstring, str)
# print result
print("Is instance a string ? : " + str(res))

Let’s see the code in detail. Firstly we created the string and printed the created string.After that checked whether the instance is string or not by using the is instance method in python and assigned the output to the variable res. Next we printed the output.

Output

The following is the output of the isinstance() method. We can observe the output in the binary format.

The original string : python
Is instance a string ? : True

Using the type() method

Another of the methods to check whether the instance is string or not is the instance method. The following syntax can be used for checking if the instance is string or not.

type(obj, basestring)

Where,

  • type is the method

  • obj is the object

  • basestring is the type of the instance

Example

Let’s see an example to check whether the given instance is a string or not by using the type method. The following code can be used for checking if the instance is string or not.

tstring = "python"
print("The original string : " + str(tstring))
# using isinstance()
# Check if variable is string 
res = (type(tstring)== str)
# print result
print("Is instance a string ? : " + str(res))

Let’s see the code in detail. Firstly we created the string and printed the created string. After that checked whether the instance is string or not by using the type method in python and assigned the output to the variable res. Next we printed the output.

Output

The following is the output of the isinstance method. We can observe the output in the binary format.

The original string : python
Is instance a string ? : True

Updated on: 15-May-2023

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements