Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect what a python string begins with [duplicate]

Tags:

python

string

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.

like image 426
user3848207 Avatar asked Oct 17 '17 02:10

user3848207


People also ask

How do you check a string starts with in Python?

Python String startswith() The startswith() method returns True if a string starts with the specified prefix(string). If not, it returns False .

How do you check if a string starts with a word in Python?

The startswith() string method checks whether a string starts with a particular substring. If the string starts with a specified substring, the startswith() method returns True; otherwise, the function returns False.

How do I check if a string has repeated characters?

If we want to know whether a given string has repeated characters, the simplest way is to use the existing method of finding first occurrence from the end of the string, e.g. lastIndexOf in java. In Python, the equivalence would be rfind method of string type that will look for the last occurrence of the substring.


1 Answers

You're looking for str.startswith

if file_string.startswith("begin_like_this"):
like image 165
Patrick Haugh Avatar answered Oct 05 '22 08:10

Patrick Haugh