October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Check If a Value Is a String in Python

Use isinstance(value, str) for the usual Python runtime string check. Learn why str(value) is conversion—not a type test—and how bytes differ from text.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use isinstance(value, str) to check whether a Python value is a string. It returns True for strings, including subclasses of str, and False for other types.

Use isinstance() for the usual runtime check

Pass the value and the str type to Python’s built-in isinstance() function:

value = "hello"

if isinstance(value, str):
    print("value is a string")
else:
    print("value is not a string")

This checks the object’s type relationship at runtime. For example, an integer does not pass the check:

value = 42
print(isinstance(value, str))  # False

The Python Software Foundation’s Programming FAQ recommends the built-in isinstance(obj, cls) function for checking whether an object is an instance of a class, including isinstance(obj, str).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When to use an exact type check

isinstance(value, str) also returns True when value is an instance of a subclass of str. If your code specifically requires the exact built-in str type and should reject subclasses, use:

type(value) is str

Choose the check that matches the requirement: use isinstance() for the normal “is this text?” question, and the exact-type check only when subclasses must be excluded.

Do not use str() to test the original type

str(value) converts a value to a string representation; it does not tell you whether the value was already a string. For instance:

value = 42
isinstance(value, str)  # False
str(value)              # '42'

Python’s built-in types documentation describes str as a constructor. Use isinstance() when you need to test the existing value without converting it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check bytes separately from text

Python’s str type represents text as immutable sequences of Unicode code points. bytes is a separate type for binary data, so a bytes value does not pass an isinstance(value, str) check:

value = b"hello"
print(isinstance(value, str))  # False

If the bytes contain text you need to work with, decode them using the encoding that matches the data:

text = value.decode("utf-8")
print(isinstance(text, str))  # True

UTF-8 is an example, not a guarantee that every byte sequence uses that encoding. Also, str(value) does not decode bytes: without encoding arguments, it produces a printable representation, such as "b'Zoot!'" for b'Zoot!', as the Python documentation explains.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Runtime checks versus type annotations

A type annotation communicates an expected type to readers and static type checkers; it does not replace a runtime check when your program must inspect an arbitrary value. For a runtime decision, use isinstance(value, str). See Python’s typing documentation for type hints.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.