Check if String Starts with Specific Word - Regex - Python


Python - Check if string starts with specific word using Regex

To check if a string starts with a word in Python, use the regular expression for "starts with" ^ and the word itself.

In this tutorial, we shall use the re.search() function to perform an expression match against the string and determine whether the string starts with a given word or not.


Examples

1. Check if given string starts with a specific word

In the following example, we will check if the given string 'Python is a programming language.' starts with a specific word 'Python'.

Python Program

import re

str = 'Python is a programming language.'
#search using regex
x = re.search('^Python', str)

if(x!=None):
	print('The line starts with \'Python\'.')
else:
	print('The line does not start with \'Python\'.')

Explanation:

  1. The program starts by importing the re module.
  2. The string str is defined as 'Python is a programming language.'.
  3. We use re.search('^Python', str) to check if the string starts with 'Python'. The ^ symbol ensures the match is at the beginning of the string.
  4. If a match is found, the program prints that the line starts with 'Python'; otherwise, it prints that it does not.

Output

The line starts with 'Python'.

If you print the output of re.search(), x, from the above example, you will get:

<re.Match object; span=(0, 6), match='Python'>

2. Negative scenario

In this example, we will check if the given string 'Python is a programming language.' starts with the word 'programming'.

Python Program

import re

str = 'Python is a programming language.'
#search using regex
x = re.search('^programming', str)

if(x!=None):
	print('The line starts with \'programming\'.')
else:
	print('The line does not start with \'programming\'.')

Explanation:

  1. The program uses the string 'Python is a programming language.'.
  2. The regex expression ^programming searches for the word 'programming' at the start of the string.
  3. Since the string starts with 'Python' and not 'programming', re.search() returns None, and the program outputs that the line does not start with 'programming'.

Output

The line does not start with 'programming'.

If you print the output of re.search(), x, from the above example, you will get:

None

If there are no matching instances for the regular expression provided, you will get None as the output for the re.search() function.


3. Check if the string starts with a word (case-insensitive)

In this example, we will check if the string 'python is a programming language.' starts with the word 'python', ignoring case sensitivity.

Python Program

import re

str = 'python is a programming language.'
#search using regex with case-insensitive flag
x = re.search('^python', str, re.IGNORECASE)

if(x!=None):
	print('The line starts with \'python\', case-insensitive check.')
else:
	print('The line does not start with \'python\'.')

Explanation:

  1. The program uses the string 'python is a programming language.'.
  2. The re.search() function is used with the re.IGNORECASE flag, allowing it to ignore case while matching the word 'python'.
  3. Since the string starts with 'python' (case-insensitive), the program will output that the line starts with 'python'.

Output

The line starts with 'python', case-insensitive check.

4. Check if the string starts with any of multiple words

In this example, we will check if the string starts with any of the words 'Python' or 'Java'.

Python Program

import re

str = 'Python is a programming language.'
#search using regex for multiple possible starting words
x = re.search('^(Python|Java)', str)

if(x!=None):
	print('The line starts with \'Python\' or \'Java\'.')
else:
	print('The line does not start with \'Python\' or \'Java\'.')

Explanation:

  1. The program uses the string 'Python is a programming language.'.
  2. The regular expression '^(Python|Java)' checks if the string starts with either 'Python' or 'Java'.
  3. Since the string starts with 'Python', the program will output that the line starts with 'Python' or 'Java'.

Output

The line starts with 'Python' or 'Java'.

Summary

In this tutorial, we learned how to check if a string starts with a specific word using Python Regular Expressions. We covered multiple scenarios including case-insensitive checks, matching multiple starting words, and the negative case where the string does not match the given word.




Python Libraries