Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
3 views
in Python by (2.6k points)

How can I check a given object is of a given type? How can I check that a object inherit a given type? 

There's an object o. How can I check that it's a string?

2 Answers

0 votes
by (46k points)
edited by

There's no need to check it in Python, but if you still want to check it isinstance(o, str) it will return true if  is a string or inherits from str.

type(o) is str will return true  only if o is str , or false  if o inherits a type str.

Hope this helps. 

0 votes
by (106k points)
edited by

To check if o is an instance of str or any subclass of str, use isinstance (this would be the "canonical" way):

if isinstance(o, str):

You can use the following video tutorials to clear all your doubts:-

Learn more about Python from an expert. Enroll in our Python Course!

Related questions

+6 votes
3 answers
+3 votes
2 answers
+3 votes
2 answers
+1 vote
2 answers
+1 vote
1 answer

Browse Categories

...