Open In App

Python | String startswith()

Last Updated : 20 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Python String startswith() Method Syntax

Syntax: str.startswith(prefix, start, end)

Parameters:

  1. prefix: prefix ix nothing but a string that needs to be checked.
  2. start: Starting position where prefix is needed to be checked within the string.
  3. end: Ending position where prefix is needed to be checked within the string.

Return:  Returns True if strings start with the given prefix otherwise returns False.

String startswith() in Python Example

Here we will check if the string is starting with “Geeks” and then it will find the string begins with “Geeks” If yes then it returns True otherwise it will return false.

Python3




var = "Geeks for Geeks"
 
print(var.startswith("Geeks"))
print(var.startswith("Hello"))


Output:

True
False

Python startswith() Without start and end Parameters

If we do not provide start and end parameters, then the Python String startswith() string method will check if the string begins with present the passed substring or not.

Python3




text = "geeks for geeks."
 
# returns False
result = text.startswith('for geeks')
print(result)
 
# returns True
result = text.startswith('geeks')
print(result)
 
# returns False
result = text.startswith('for geeks.')
print(result)
 
# returns True
result = text.startswith('geeks for geeks.')
print(result)


Output: 

False
True
False
True

Python startswith() With start and end Parameters

If we provide start and end parameters, then startswith() will check, if the substring within start and end start matches with the given substring.

Python3




text = "geeks for geeks."
 
result = text.startswith('for geeks', 6)
print(result)
 
result = text.startswith('for', 6, 9)
print(result)


Output:

True
True

Check if a String Starts with a Substring

We can also pass a tuple instead of a string to match within the Python String startswith() Method. In this case, startswith() method will return True if the string starts with any of the items in the tuple.

Python3




string = "GeeksForGeeks"
res = string.startswith(('geek', 'geeks', 'Geek', 'Geeks'))
print(res)
 
string = "apple"
res = string.startswith(('a', 'e', 'i', 'o', 'u'))
print(res)
 
string = "mango"
res = string.startswith(('a', 'e', 'i', 'o', 'u'))
print(res)


Output:

True
True
False


Previous Article
Next Article

Similar Reads

Numpy string operations | startswith() function
numpy.core.defchararray.startswith() function returns a boolean array which is True where the string element in starts with prefix, otherwise False. Syntax : numpy.core.defchararray.startswith(arr, prefix, start = 0, end = None) Parameters : arr : [array-like of str or unicode] Array-like of str. prefix : [str] Input string. start, end : [int, opti
1 min read
Python | startswith() and endswith() functions
Python library provides a number of built-in methods, one such being startswith() and endswith() functions which are used in string related operations. Startswith() Syntax: str.startswith(search_string, start, end) Parameters : search_string : The string to be searched. start : start index of the str from where the search_string is to be searched.
2 min read
Python | Pandas Series.str.startswith()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas startswith() is yet another method to search and filter text data in Series or Data Frame. This method is Similar to Python's sta
3 min read
String slicing in Python to check if a string can become empty by recursive deletion
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again. Examples: Input : str = "GEEGEEKSKS", sub_str = "GEEKS" Output : Yes Explanation :
2 min read
String slicing in Python to Rotate a String
Given a string of size n, write functions to perform following operations on string. Left (Or anticlockwise) rotate the given string by d elements (where d <= n).Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeek
3 min read
Python | Check if given string can be formed by concatenating string elements of list
Given a string 'str' and a list of string elements, write a Python program to check whether given string can be formed by concatenating the string elements of list or not. Examples: Input : str = 'python' lst = ['co', 'de', 'py', 'ks', 'on'] Output : False Input : str = 'geeks' lst = ['for', 'ge', 'abc', 'ks', 'e', 'xyz'] Output : True Approach #1
5 min read
Python | Check if string ends with any string in given list
While working with strings, their prefixes and suffix play an important role in making any decision. For data manipulation tasks, we may need to sometimes, check if a string ends with any of the matching strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using filter() + endswith() The combination of the above func
6 min read
Python | Sorting string using order defined by another string
Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once. Examples: Input : pat = "asbcklfdmegnot", str = "eksge" Output : str = "g
2 min read
Python | Merge Tuple String List values to String
Sometimes, while working with records, we can have a problem in which any element of record can be of type string but mistakenly processed as list of characters. This can be a problem while working with a lot of data. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension + join() The combination of abov
6 min read
Python | Sort each String in String list
Sometimes, while working with Python, we can have a problem in which we need to perform the sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + sorted() + join() This is
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg