Python3 startswith() method

Python3 字符串Python3 String


Description

The startswith() method is used to check whether a string starts with the specified substring. If so, it returns True, otherwise it returns False. If the beg and end parameters specify values, the check is performed within the specified range.

Syntax

startswith() method syntax:

str.startswith(substr, beg=0,end=len(string));

Parameters

  • str -- The string to be detected.
  • substr -- The specified substring.
  • strbeg -- Optional parameter used to set the starting position of the string detection.
  • strend -- Optional parameter used to set the ending position of the string detection.

Return value

Returns True if the string is detected, otherwise returns False.

Example

The following example demonstrates the usage of the startswith() function:

React example

#!/usr/bin/python3 str = "this is string example....wow!!!" print (str.startswith( 'this' )) # Whether the string starts with this print (str.startswith( 'string', 8 )) # Whether the string starting from the ninth character starts with string print (str.startswith( 'this', 2, 4 )) # Whether the string starting from the 2nd character to the fourth character starts with this

The output of the above example is as follows:

True
True
False

Python3 字符串Python3 String

Other extensions