.endswith()
Published Mar 15, 2022
Contribute to Docs
The .endswith()
method checks a value against a given string and returns True
if the string ends with that value. Otherwise, it returns False
.
Syntax
string.endswith(value, start, end)
Given a string, the .endswith()
method can be used in the following way:
The required
value
argument, which is checked if it exists at the end of the string. It is also case-sensitive.Optionally, the
value
can be tested for whether it is at the end of a particular substring within the string using thestart
andend
index arguments.
Example
example_str = "This is a string"check_A = example_str.endswith("g")check_B = example_str.endswith("s")check_C = example_str.endswith("st", 5, 12)print("A: ", check_A)print("B: ", check_B)print("C: ", check_C)
The output will look like this:
A: TrueB: FalseC: True
From the above example, for check_A
the output is True
as the .endswith()
function checks whether the example_str
string ends with the character “g”.
Codebyte Example
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours