Remember
endswith() tests whether text ends with a suffix. It is case-sensitive unless you normalize the text first.
endswith() tests whether text ends with a suffix. It is case-sensitive unless you normalize the text first.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'main.py'
result = text.endswith(".py")
print(result)Top-level scope
'main.py'Object 2 · boolTrueendswith() tests whether text ends with a suffix. It is case-sensitive unless you normalize the text first.
(no output)
What do you expect this program to print?
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: TrueChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'main.py'
result = text.endswith(".py")
print(result)Top-level scope
'main.py'Object 2 · boolTrueThe example has finished. Compare the final output with the values and references above.
True
What do you expect this program to print?
text = 'main.py'
result = text.endswith(".py")
print(result)String endswith() MethodThe 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.
string.endswith(suffix[, start[, end]])
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).True if the string ends with the specified suffix, otherwise False.text = "hello world"
print(text.endswith("world"))
True
text = "python programming"
print(text.endswith("programming", 7))
True
filename = "report.pdf"
print(filename.endswith((".pdf", ".docx")))
True
This checks if the string ends with .pdf or .docx.
.jpg, .pdf)TypeErrorstring.endswith is a reference, not a function callIn 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.
string.endswith() checks how a string endsTrue or Falsestart and end rangeWrite 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.")
Connect what you noticed in the example with the idea below.
endswith() tests whether text ends with a suffix. It is case-sensitive unless you normalize the text first.
endswith() ignores letter case by default.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.