How to check whether a string starts with XYZ in Python?


A string is a collection of characters stored as a single value. Un like other technologies there is no need to explicitly declare strings in python (for that matter any variable), you just need to assign strings to a literal this makes Python strings easy to use.

In Python, a string is represented by the class named String. This class provides several functions and methods using which you can perform various operations on strings.

In this article, we are going to find out how to check whether a string starts with XYZ in Python.

Using startswith() method

One way to achieve this is using the inbuilt startswith() method. The String class in Python contains a function called startswith(string). This function is executed on a string object and receives a prefix string that you want to search.

This method is given to a string and the substring is given as parameter, if the string is starting with the substring then True is returned, otherwise False is returned.

Example 1

In the example given below, we are taking a string and a substring as input and we are checking whether the string is starting with a substring or not using the startswith() method.

str1 = "Welcome to Tutorialspoint" print("The given string is") print(str1) substr = "Wel" print("The given substring is") print(substr) print("Checking if the string is starting with the substring") print(str1.startswith(substr))

Output

The output of the above example is,

The given string is
Welcome to Tutorialspoint
The given substring is
Wel
Checking if the string is starting with the substring
True

Example 2

In the example given below, we are taking the same program as above but with different inputs and checking if the string starts with the substring or not.

str1 = "Welcome to Tutorialspoint" print("The given string is") print(str1) substr = "XYZ" print("The given substring is") print(substr) print("Checking if the string is starting with the substring") print(str1.startswith(substr))

Output

The output of the above example is,

The given string is
Welcome to Tutorialspoint
The given substring is
XYZ
Checking if the string is starting with the substring
False

Using regular expressions

Regular expressions are used in the second technique. Import the re library and install it if it isn't already installed to use it. After importing the re library, we'll utilize the regular expression "^substring." The re.search() function examines if the text begins with the specified substring using a regular expression.

Example 1

In the example given below, we are taking a string and a substring as inputs and we are checking if the string is starting with the substring using re.search method.

import re str1 = "Welcome to Tutorialspoint" print("The given string is") print(str1) substr = "Wel" print("The given substring is") print(substr) print("Checking if the string is starting with the substring") print(bool(re.search("^Wel", str1)))

Output

The output of the above example is,

The given string is
Welcome to Tutorialspoint
The given substring is
Wel
Checking if the string is starting with the substring
True

Example 2

In the example given below, we are taking the same program as above but we are taking different inputs and checking if the string is starting with the substring

import re str1 = "Welcome to Tutorialspoint" print("The given string is") print(str1) substr = "XYZ" print("The given substring is") print(substr) print("Checking if the string is starting with the substring") print(bool(re.search("^XYZ", str1)))

Output

The output of the above example is,

The given string is
Welcome to Tutorialspoint
The given substring is
XYZ
Checking if the string is starting with the substring
False

Updated on: 26-Oct-2022

313 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements