A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-string-startswith/ below:

Python - String startswith() - GeeksforGeeks

Python - String startswith()

Last Updated : 29 Apr, 2025

startswith() method in Python checks whether a given string starts with a specific prefix. It helps in efficiently verifying whether a string begins with a certain substring, which can be useful in various scenarios like:

Let’s understand by taking a simple example:

Python
s = "GeeksforGeeks"
res = s.startswith("for")

print(res)  

Explanation: startswith() method checks if the string s starts with "for", and since it starts with "Geeks" instead of for, the result is False.

Syntax

string.startswith(prefix[, start[, end]])

Parameters:

Return Type: The method returns a Boolean:

Examples of startswith() method Example 1: Basic Prefix Check

Here’s a simple example where we check if a given string starts with the word "for".

Python
s = "GeeksforGeeks"

res = s.startswith("for")
print(res)  

Explanation: The string starts with "Geeks", so it returns True.

Example 2: Using start Parameter

Let’s check the string using the start parameter to begin from a specific index.

Python
s = "GeeksforGeeks"
print(s.startswith("for", 5))

Explanation: We're checking from index 5, and "for" starts from there hence the result is True.

Example 3: Using start and end Parameters

We can also check for stings within a given slice using star and end parameters.

Python
s = "GeeksforGeeks"
print(s.startswith("for", 5, 8))

Explanation: checking only between index 5 and 8 (non inclusive) and "for" lies in that slice, so it returns True.

Example 4: Checking Multiple Prefixes

We can also check if the string starts with any one of several prefixes by passing a tuple of prefixes.

Python
s = "GeeksforGeeks"

res = s.startswith(("Geeks", "G"))
print(res)

Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4