37

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.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
user3848207
  • 3,737
  • 17
  • 59
  • 104
  • 3
    I did google but used the wrong keyword and could not find useful searches. I used "begin with" instead of "start with". – user3848207 Oct 17 '17 at 03:06
  • 10
    Recommending "google it" for API questions is bad advice, for _any_ language. This is what the API docs are for. Break the google/SO cycle (google links to SO, SO says "google it"). Link to the docs (like the accepted answer does.) Forum responsibly. – michael Oct 28 '17 at 07:06

1 Answers1

63

You're looking for str.startswith

if file_string.startswith("begin_like_this"):
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96