Python Tip #92 (of 365):

Don't overuse regular expressions

If a containment check of a common string method will do, don't use a regular expression.

Instead of this:
if re‍.search(r"^http://", string):

Do this:
if string.startswith("http://"):

Instead of this:
if re‍.search(r"#", string):

Do this:
if "#" in string:

And instead of this:
parts = re.findall(r"\S+", string)

Do this:
parts = string.split()

🧡 (1/2)

#Python #DailyPythonTip

Don't use regular expressions for very simple operations.

If the "in" operator or a string method will do what you need in an easier to understand fashion, just use those!

Seem like I'm contradicting tip 89? Check it again. I find those methods less understandable than regular expressions.

https://pym.dev/string-methods/

🧡 (2/2)

Python's String Methods

Python's strings have dozens of methods, but some are much more useful than others. Let's discuss the dozen-ish must-know string methods and why the other methods aren't so essential.

@treyhunner native Python string operations are faster too