Detect what a python string begins with

Question:

To detect whether a python string ends with a particular substring, say ".txt", there is a convenient built-in python string method;

 if file_string.endswith(".txt"):

I would like to to detect whether a python string begins with a particular substring, say "begin_like_this". I am not able find a convenient method that I can use like this;

 if file_string.beginswith("begin_like_this"):

I am using Python 3.6.

Asked By: user3848207

||

Answers:

You’re looking for str.startswith

if file_string.startswith("begin_like_this"):
Answered By: Patrick Haugh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.