A RetroSearch Logo

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

Search Query:

Showing content from https://www.tutorialspoint.com/python/string_startswith.htm below:

Python String startswith() Method

Python String startswith() Method

The Python string method startswith() checks whether string starts with a given substring or not. This method accepts a prefix string that you want to search for and is invoked on a string object.

The method can also limit the search range by defining the indices where the search begins and ends, i.e. the user can decide where in the string can start the search and terminate it. So, even if the substring does not start the string, but it begins from the specified limit set, the method returns true.

Syntax

Following is the syntax for Python String startswith() method −

str.startswith(str, beg=0,end=len(string));
Parameters Return Value

This method returns true if found matching string otherwise false.

Example

Without passing optional parameters, the method checks whether the string input starts with the substring parameter passed. If yes, it returns true.

The following example shows the usage of Python String startswith() method. Here, we are creating a string "this is string example....wow!!!" and call the startswith() method on it. We pass a substring to the method as its argument and record the return value as follows -

str = "this is string example....wow!!!";
print str.startswith( 'this' )
print str.startswith( 'is' )

When we run above program, it produces following result −

True
False
Example

When we pass a substring and optional (start, end) parameters to the method, it returns true if the string input starts with the given substring from the given start index.

In this example, we create a string "this is string example....wow!!!". Then, we call the startswith() method on it. We pass the substring, start and end arguments to it as follows −

str = "this is string example....wow!!!";
print str.startswith( 'this', 3, 10 )
print str.startswith( 'is', 2, 8 )

When we run above program, it produces following result −

False
True
Example

In the following example, we create two strings str1 = "Hello Tutorialspoint" and str2 = "Hello". Using conditional statements, we check the return value of the startswith() method, called on string str1 by passing string str2 as the argument to it.

str1 = "Hello Tutorialspoint"
str2 = "Hello"
if str1.startswith(str2):
    print("The string starts with " + str2)
else:
    print("The string does not start with " + str2)

When we run above program, it produces following result −

The string starts with Hello
Example

In the following example we pass the substring and optional parameters, to compare with the input string; and using conditional statements, we print the return value.

str1 = "Tutorialspoint"
str2 = "Tutorials"
if str1.startswith(str2):
    print("The string starts with " + str2)
else:
    print("The string does not start with " + str2)

When we run above program, it produces following result −

The string does not start with Tutorials

python_strings.htm


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