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.
Answers:
You’re looking for str.startswith
if file_string.startswith("begin_like_this"):
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.
You’re looking for str.startswith
if file_string.startswith("begin_like_this"):