.startswith()
Published Jan 19, 2023
Contribute to Docs
The .startswith()
method checks a value against a given string and returns True
if the string starts with that value. Otherwise, it returns False
.
Syntax
string.startswith(value, start, end)
Given a string, the .startswith()
method can be used in the following way:
- The required
value
argument is checked for existence at the beginning of thestring
. It is also case-sensitive. - Optionally, a
start
andend
index argument can be declared. Thevalue
is then checked for existence at the beginning of the substring from thestring
‘sstart
andend
indices.
Example
example_str = "This is a string"check_A = example_str.startswith("T")check_B = example_str.startswith("t")print("A: ", check_A)print("B: ", check_B)
The output will look like this:
A: TrueB: False
In the example above, check_A
is True
since the example_str
string starts with the character "T"
.
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