[SOLVED] Detect what a python string begins with

Issue

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.

Solution

You’re looking for str.startswith

if file_string.startswith("begin_like_this"):

Answered By – Patrick Haugh

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *