Python Reference Library →

Python String startswith() Method – Check If a String Starts With

Let’s look at string startswith() method – check if a string starts with

  1. Notice

    startswith() tests a case-sensitive prefix. Optional bounds limit the part of the string examined.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'Python'
result = text.startswith("Py")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'Python'Object 2 · boolTrue
This step

startswith() tests a case-sensitive prefix. Optional bounds limit the part of the string examined.

Picture string startswith() method – check if a string starts with

startswith() tests a case-sensitive prefix. Optional bounds limit the part of the string examined.

Image brief

Alt text
A diagram for Python String startswith() Method – Check If a String Starts With. Instructions: text = 'Python' result = text.startswith("Py") print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: 'Python'; object 2: True
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String startswith() Method – Check If a String Starts With". Teaching focus: startswith() tests a case-sensitive prefix. Optional bounds limit the part of the string examined. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'Python' result = text.startswith("Py") print(result) | v DISPLAYED RESULT True FINAL BINDINGS text --> object 1 (str) result --> object 2 (bool) object 1: 'Python' object 2: True Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String startswith() Method – Check If a String Starts With. Instructions: text = 'Python' result = text.startswith("Py") print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: 'Python'; object 2: True

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'Python'
result = text.startswith("Py")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'Python'Object 2 · boolTrue
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
True

What do you expect this program to print?

What does this example display?

text = 'Python'
result = text.startswith("Py")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

True

Follow the printed values in order. The output is: True

More examples and reference

Python String startswith() Method

The startswith() method in Python is used to check if a string starts with a specific substring (prefix). It returns True if the string starts with the given value, and False otherwise.

Syntax

string.startswith(prefix[, start[, end]])

Parameters:

  • prefix: The substring you want to check at the beginning.
  • start (optional): The position to start checking from.
  • end (optional): The position to stop checking (not included).

Returns:

  • True if the string starts with the specified prefix; otherwise, False.

Example 1: Basic Usage

text = "hello world"
print(text.startswith("hello"))
True

Example 2: Case Sensitivity

print("Python".startswith("py"))
False

The method is case-sensitive.

Example 3: Using Start and End Parameters

msg = "Learn Python Programming"
print(msg.startswith("Python", 6))
True

It starts checking from index 6.

Example 4: Tuple of Prefixes

website = "https://example.com"
print(website.startswith(("http://", "https://")))
True

You can pass a tuple of prefixes to check against multiple options.

Common Use Cases

  • Checking if a URL starts with "https://"
  • Filtering strings by prefix (e.g., files starting with "log_")
  • Text parsing or log filtering

Common Mistakes

  • Assuming it's case-insensitive – use .lower() or .upper() to handle case.
  • Passing an empty string – returns True because every string starts with "".

Interview Tip

In interviews, startswith() is often used in filtering, log analysis, and string-matching problems.

Summary

  • startswith() checks if a string starts with a given prefix.
  • Supports optional start and end positions.
  • Returns True or False.
  • Is case-sensitive.

Practice Problem

Write a program that reads a filename from the user and checks if it starts with "img_".

filename = input("Enter filename: ")
if filename.startswith("img_"):
    print("This is an image file.")
else:
    print("Not an image file.")

What to remember

Connect what you noticed in the example with the idea below.

Remember

startswith() tests a case-sensitive prefix. Optional bounds limit the part of the string examined.

Watch out for this idea

startswith() ignores letter case by default.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →