Python Reference Library →

Python String endswith() – Check if a String Ends with Substring

Let’s look at string endswith() – check if a string ends with substring

  1. Notice

    endswith() tests whether text ends with a suffix. It is case-sensitive unless you normalize the text first.

  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 = 'main.py'
result = text.endswith(".py")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'main.py'Object 2 · boolTrue
This step

endswith() tests whether text ends with a suffix. It is case-sensitive unless you normalize the text first.

Picture string endswith() – check if a string ends with substring

endswith() tests whether text ends with a suffix. It is case-sensitive unless you normalize the text first.

Image brief

Alt text
A diagram for Python String endswith() – Check if a String Ends with Substring. Instructions: text = 'main.py' result = text.endswith(".py") print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: 'main.py'; object 2: True
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String endswith() – Check if a String Ends with Substring". Teaching focus: endswith() tests whether text ends with a suffix. It is case-sensitive unless you normalize the text first. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'main.py' result = text.endswith(".py") print(result) | v DISPLAYED RESULT True FINAL BINDINGS text --> object 1 (str) result --> object 2 (bool) object 1: 'main.py' 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 endswith() – Check if a String Ends with Substring. Instructions: text = 'main.py' result = text.endswith(".py") print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: 'main.py'; 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 = 'main.py'
result = text.endswith(".py")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'main.py'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 = 'main.py'
result = text.endswith(".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 endswith() Method

The endswith() method in Python checks whether a string ends with a specific substring (also called a suffix). It returns True if the string ends with the given value, otherwise False.

Syntax

string.endswith(suffix[, start[, end]])

Parameters:

  • suffix – The substring to check for at the end of the string. Can be a single string or a tuple of strings.
  • start (optional) – The starting index to begin searching (default is 0).
  • end (optional) – The ending index to stop searching (default is end of the string).

Returns:

  • True if the string ends with the specified suffix, otherwise False.

Example 1: Basic Usage

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

Example 2: Using start and end

text = "python programming"
print(text.endswith("programming", 7))
True

Example 3: Tuple of Suffixes

filename = "report.pdf"
print(filename.endswith((".pdf", ".docx")))
True

This checks if the string ends with .pdf or .docx.

Use Cases

  • Validating file extensions (e.g., .jpg, .pdf)
  • Filtering URLs that end in certain paths
  • Checking strings in data validation tasks

Common Mistakes

  • Passing non-string types as suffix → causes TypeError
  • Not using parentheses: string.endswith is a reference, not a function call

Interview Tip

In coding challenges, endswith() is useful when you need to filter or process strings based on a pattern at the end, like file paths or text parsing.

Summary

  • string.endswith() checks how a string ends
  • Returns True or False
  • Supports optional start and end range
  • Can accept a tuple of possible endings

Practice Problem

Write a program that checks if a user-entered filename ends with ".txt".

filename = input("Enter the file name: ")
if filename.endswith(".txt"):
    print("This is a text file.")
else:
    print("This is not a text file.")

What to remember

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

Remember

endswith() tests whether text ends with a suffix. It is case-sensitive unless you normalize the text first.

Watch out for this idea

endswith() ignores letter case by default.

This is the misconception to avoid.

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

    Reference library →