Python USES the startswith of function to determine the beginning of a string

  • 2020-05-07 19:54:19
  • OfStack

Function: startswith ()

Determines whether a string begins with a specified character or substring

1. Function description
syntax: string.startswith (str, beg=0,end=len(string))
          or string[beg:end].startswith(str)
 
Parameter description:
string: the string to which   is detected
str: the character or substring specified by          . (you can use tuples, which match 1 by 1)
beg:       sets the starting position for string detection (optional)
end:       sets the end position of the string detection (optional)
If the parameters beg and end exist, it is checked within the specified range, otherwise it is checked throughout the string

The return value
If a string is detected, True is returned, otherwise False is returned. The default null character is True

Function resolution: if the string string starts with str, it returns True, otherwise False


2.
instance


>>> s = 'hello good boy doiido'
>>> print s.startswith('h')
True
>>> print s.startswith('hel')
True
>>> print s.startswith('h',4)
False
>>> print s.startswith('go',6,8)
True

# Matching null character set 
>>> print s.startswith('')
True
# Match the tuple 
>>> print s.startswith(('t','b','h'))
True


Common environment: for if judgment


>>> if s.startswith('hel'):
 print "you are right"
else:
 print "you are wrang"

you are right


Related articles: