Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

How to Check if a String Starts with a Substring in Python

Use Python’s startswith() method to test a string prefix, with examples for tuples, positions, caseless Unicode checks, prefix removal, and binary data.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Python’s built-in str.startswith() method. It returns True when a string begins with the given prefix and False otherwise.

Check for one prefix

Call startswith() on the string and pass the prefix you want to test:

text = "Python programming"
prefix = "Python"

if text.startswith(prefix):
    print("The string starts with the prefix")

The method’s form is str.startswith(prefix[, start[, end]]). The comparison uses the text as provided, so it is case-sensitive: "Python" matches the beginning of "Python programming", but not "python programming". Python’s built-in types documentation describes the method and its return value.

Check for one of several prefixes

Pass a tuple when any one of several complete prefixes should count as a match:

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.
url = "https://example.com"

if url.startswith(("http://", "https://")):
    print("The URL uses a supported scheme")

Each tuple member is checked as a candidate prefix. A tuple is not a set of individual characters; for example, ("http://", "https://") tests those two full beginnings.

Check a prefix at a particular position

Use the optional start and end arguments to limit the comparison to a region of the string. For example, this checks whether "programming" begins at position 7:

text = "Python programming"

text.startswith("programming", 7)  # True

The range arguments let you make the check without first creating a sliced string. The documentation establishes the range behavior; it does not establish a performance advantage.

Perform a caseless Unicode prefix check

startswith() does not ignore case automatically. For caseless Unicode matching, casefold both the string and the prefix before checking:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
text = "Straße"
prefix = "STRASSE"

text.casefold().startswith(prefix.casefold())  # True

Python documents casefolded strings as useful for caseless matching. Casefolding is more aggressive than lowercasing; for example, "straße".lower() remains "straße", while "straße".casefold() becomes "strasse". See the Python documentation for str.casefold().

Remove a matching prefix instead of checking it

If you need the resulting string with a matching prefix removed, use removeprefix():

text = "TestHook"

text.removeprefix("Test")  # "Hook"

If the prefix does not match, removeprefix() returns the original string. It was added in Python 3.9. Use startswith() when you only need a yes-or-no check. Python documents removeprefix() and its version history.

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

Check a prefix in binary data

bytes and bytearray also provide startswith(). Pass a bytes-like prefix, or use a tuple of prefixes and the optional range positions:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
data = b"Python data"
data.startswith(b"Python")  # True

For these binary types, use a bytes-like prefix such as b"Python", rather than a text string. The Python documentation covers bytes and bytearray prefix checks.

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
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

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.