---
title: "str.startswith"
space: "Student Handbook"
url: "https://portal.cs17.org/student-handbook/python-manual/strings/startswith"
updated: "2026-10-05"
---

Checks how a string begins (or, with `endswith`, how it ends).

## Synopsis

```python
text.startswith(prefix)
text.endswith(suffix)
```

## Description

Both return `True` or `False`, so they fit straight into an `if`.

## Parameters

| Name | Meaning |
| --- | --- |
| `prefix / suffix` | The text to look for. It can also be a tuple of choices: `("y", "Y")`. |

## Return value

A `bool`.

## Example

```python
print("hello.py".endswith(".py"))
print("Hello".startswith("He"))
print("Hello".startswith("he"))
```

It prints:

```text
True
True
False
```

## Common mistake

```python
answer = "Yes"
print(answer.startswith("y" or "Y"))
```

It prints:

```text
False
```

`"y" or "Y"` is worked out first and is just `"y"`. Write `answer.lower().startswith("y")` instead.

## See also

[in](/student-handbook/python-manual/strings/in), [str.lower](/student-handbook/python-manual/strings/lower)

[str.startswith in the Python documentation](https://docs.python.org/3/library/stdtypes.html#str.startswith)
