Check if String starts with special characters in Python

This article will discuss different ways to check if a string starts with one or more special characters in Python.

Table Of Content

Check if String starts with any special character using Regex

In Python, the regex module provides a function search(str, pattern). It accepts a string and regex pattern as arguments. Then it scans through the string and looks for the given regex pattern. If a match to the pattern is found, then it returns the Match object; otherwise, it returns None. We can use this to check if a string starts with any special character or not. For that, we will use the following regex pattern,

import string

pattern = r'^[' + string.punctuation + ']+'

In Python, the string module provides a variable punctuation that contains all the special characters i.e. r”””!”#$%&'()*+,-./:;<=>[email protected][]^_`{|}~”””. We used that string to create the regex pattern. This pattern checks if the string starts with one or more special characters. Let’s see some examples,

Example 1:

Advertisements
import re
import string

# Create a regex pattern to match a string
# that starts with one or more special characters
pattern = r'^[' + string.punctuation + ']+'

sample_str = "$sample string"

# Check if string starts with special characters
if re.search(pattern, sample_str) is not None:
    print("The String starts with special characters")
else:
    print("The String do not starts with special characters")

Output

The String starts with special characters

Our string has a special character at the beginning, and the regex pattern successfully detected that.

Example 2:

import re
import string

# Create a regex pattern to match a string
# that starts with one or more special characters
pattern = r'^[' + string.punctuation + ']+'

sample_str = "sample string"

# Check if string starts with special characters
if re.search(pattern, sample_str) is not None:
    print("The String starts with special characters")
else:
    print("The String do not starts with special characters")

Output

The String do not starts with special characters

Our string had no special character at the starting. It was a negative test.

Check if String starts with any special character without Regex

Select the string’s first character using the subscript operator i.e. str[0]. Then check if that character matches any special character in “string.punctuation”. If yes, then it means that our string starts with a special character, otherwise not. Let’s see some examples,

Example 1:

import string

sample_str = "%%sample string"

# Check if string starts with a special character
if sample_str[0] in string.punctuation:
    print("The String starts with a special character")
else:
    print("The String do not starts with a special character")

Output

The String starts with special characters

Our string has a special character at the beginning, and the regex pattern successfully detected that.

Example 2:

import string

sample_str = "sample string"

# Check if string starts with a special character
if sample_str[0] in string.punctuation:
    print("The String starts with a special character")
else:
    print("The String do not starts with a special character")

Output

The String do not starts with special characters

Our string had no special character at the starting. It was a negative test.

Summary:

We learned two different ways to check if a string starts with special characters in Python.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top