Use isinstance to check if o is an instance of str or any subclass of str:

if isinstance(o, str):

To check if the type of o is exactly str, excluding subclasses of str:

if type(o) is str:

See Built-in Functions in the Python Library Reference for relevant information.


Checking for strings in Python 2

For Python 2, this is a better way to check if o is a string:

if isinstance(o, basestring):

because this will also catch Unicode strings. unicode is not a subclass of str; both str and unicode are subclasses of basestring. In Python 3, basestring no longer exists since there's a strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return True if o is an instance of any subclass of any of (str, unicode):

if isinstance(o, (str, unicode)):
Answer from Fredrik Johansson on Stack Overflow
🌐
Real Python
realpython.com › python-type-checking
Python Type Checking (Guide) – Real Python
January 7, 2019 - Variable annotations were defined in PEP 526 and introduced in Python 3.6. The syntax is the same as for function argument annotations: ... The variable pi has been annotated with the float type hint. Note: Static type checkers are more than able to figure out that 3.142 is a float, so in this example the annotation of pi is not necessary.
🌐
TestDriven.io
testdriven.io › blog › python-type-checking
Python Type Checking | TestDriven.io
December 1, 2023 - You can similarly use a decorator for functions like so: from typeguard import typechecked @typechecked def sum_ab(a: int, b: int) -> int: return a + b · It also comes with a pytest plugin. To check types for package my_package while running tests you can run this command: $ python -m pytest --typeguard-packages=my_package
Discussions

How to check for a function type in Python? - Stack Overflow
I've got a list of things, of which some can also be functions. If it is a function I would like to execute it. For this I do a type-check. This normally works for other types, like str, int or flo... More on stackoverflow.com
🌐 stackoverflow.com
Python type checking for functions - Stack Overflow
I'm having some issues figuring out how to mark up functions for type checking. Let's say I have the following situation: class BaseClass: def __init__(self): self.name = 'base' class More on stackoverflow.com
🌐 stackoverflow.com
Type checking in Python - Stack Overflow
How can I check the type of function's parameters? For example, def hello(x): # check if x is tuple # check if x is dictionary. ... ... Save this answer. ... Show activity on this post. ... You should never be unsure of the type of a thing in python; it's basically impossible to check accurately. More on stackoverflow.com
🌐 stackoverflow.com
Function that creates types compatible with static type checking - Typing - Discussions on Python.org
I would like to implement a function that returns a new type. And the returned type should be compatible with static type checkers. The initial idea I had was for the function to have a -> type return. However, this does not work because type checkers consider the assigned value to be a variable. More on discuss.python.org
🌐 discuss.python.org
0
September 27, 2023
🌐
DataCamp
datacamp.com › tutorial › type-checking-in-python
Type Checking in Python Tutorial | DataCamp
May 5, 2020 - 'mypy' is a python module which helps in a static type checking, which uses the Python's own dynamic checking or duck typing with the type-hinting. You need to install 'mypy' packages pip install mypy · You can create a file named 'mypy_example.py' in your local machine and type the following code. The below code is a simple program that accepts two integers as an input in the parameter and after '->' shows the returned data type, which is also an 'int'. However, the function needs to return int, but string 'Subtracted two integers' is returned.
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter10.04-Type-Checking.html
Type Checking — Python Numerical Methods
Try your function for erroneous input arguments to verify that they are checked. def my_adder(a, b, c): # type check if isinstance(a, float) and isinstance(b, float) and isinstance(c, float): pass else: raise(TypeError('Input arguments must be floats')) out = a + b + c return out
🌐
Python
typing.python.org › en › latest › spec › directives.html
Type checker directives — typing documentation
Type checkers should aim to minimize cases where they reject assert_type() calls that use equivalent types. The second argument must be a valid type expression. The function reveal_type(obj) makes type checkers reveal the inferred static type of an expression.
Find elsewhere
🌐
LabEx
labex.io › tutorials › python-how-to-perform-type-checking-in-python-398052
How to perform type checking in Python | LabEx
In this example, we use the typing module to specify that the calculate_average() function expects a list of floats as input and returns a float. We also use type annotations to indicate that the result is a tuple of two floats.
🌐
Medium
john-tucker.medium.com › type-checking-python-306ad8339da1
Type Checking Python. Learning that we can have our cake… | by John Tucker | Medium
November 22, 2021 - On the other hand, let us say we wrote the same example using type checking. ... As before, we change the behavior of the read_metadata method to instead return None instead of an empty Dict[str, MetadataItem] when the Resource object’s metadata does not have any of the items as keys. With this change, we can immediately see a problem with the has_name function in our editor (here with VSCode enabled with Python type checking).
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
When a TypeIs function returns False, type checkers can narrow the type of the variable to exclude the TypeIs type. Added in version 3.10. ... Typing operator to conceptually mark an object as having been unpacked. For example, using the unpack operator * on a type variable tuple is equivalent to using Unpack to mark the type variable tuple as having been unpacked:
🌐
Kansas State University
textbooks.cs.ksu.edu › cc410 › z-examples › 01-hello-real-world › 04-python › 09-type-checking
Type Checking :: CC 410 Textbook
September 10, 2025 - To add type hinting, we need to add information to each variable and function at a bare minimum. So, let’s look at the function first. Recall that functions can return a value using the return keyword. We don’t see that in our main function, so it doesn’t return a value, right? In fact, any Python function that doesn’t explicitly return a value within the code actually returns the value None by default.
🌐
DEV Community
dev.to › on_stash › type-checking-in-python-1mcn
Type-checking in Python - DEV Community
June 6, 2018 - Type checking function arguments is a common sanity check for function arguments in Python.
🌐
Mouse Vs Python
blog.pythonlibrary.org › home › type checking in python
Type Checking in Python - Mouse Vs Python
October 5, 2021 - def some_function(a, b, c): # type: (str, int, int) -> None print(a) Some Python IDEs may support type hinting in the docstring instead. PyCharm lets you do the following for example:
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-type
Python type() Function Explained | DigitalOcean
Learn how to use Python’s type() function to check data types and create dynamic classes. Includes syntax, examples, and practical tips.
🌐
CodeGenes
codegenes.net › blog › how-to-check-typeof-in-python
Mastering Type Checking in Python — codegenes.net
num = 20 # Check if num is an instance of int print(isinstance(num, int)) # Output: True string = "World" # Check if string is an instance of str print(isinstance(string, str)) # Output: True my_list = [4, 5, 6] # Check if my_list is an instance of tuple print(isinstance(my_list, tuple)) # Output: False · Every object in Python has a __class__ attribute that refers to the class to which the object belongs. value = 3.14 print(value.__class__) # Output: <class 'float'> When writing functions that accept user input or data from external sources, type checking can be used to ensure that the input is of the expected type.
🌐
Digis
digiscorp.com › python-check-type-of-variable-top-3-methods-explained
Python Check Type of Variable: Top 3 Methods Explained
December 9, 2024 - For that reason, type checking and data validation should be treated as related but separate concerns. The built-in type() function is the most direct way to find the exact type of an object in Python. When passed a variable, it returns the class to which that object belongs. A basic example is ...
🌐
Python.org
discuss.python.org › typing
Function that creates types compatible with static type checking - Typing - Discussions on Python.org
September 27, 2023 - I would like to implement a function that returns a new type. And the returned type should be compatible with static type checkers. The initial idea I had was for the function to have a -> type return. However, this does not work because type checkers consider the assigned value to be a variable.
🌐
CodeRivers
coderivers.org › blog › python-type-check
Python Type Checking: A Comprehensive Guide - CodeRivers
February 22, 2026 - Python 3.5 introduced type hints, which allow you to annotate the types of variables and functions. Here's a simple example: