in python startsWith () and endsWith () method

 startswith () method


Are Python startswith () method is used to check the string begins with a specified substring
if so returns True, otherwise False. If beg and end parameter specified value, it is checked in the specified range.
str.startswith (str, beg = 0, end = len (string));


Parameters
str - string detection.
strbeg - Optional Parameters for setting the starting position of the detected character string.
strend - optional argument string set end position detection.


The return value
if the detected string is returned True, otherwise False.

Common Environment: it means for determining IF

#!/usr/local/bin/python
# coding=utf-8
listsql = 'select * from ifrs.indiv_info'
def isSelect(sql):
    chsql = sql.upper().strip()
    if not chsql.startswith("SELECT "):
        return False
    return True

print isSelect(listsql)
[root@bigdata-poc-shtz-3 zw]# python h.py
True

 



endsWith () method of

action: determining whether the specified character string or a substring ends, commonly used to determine the file type of

a function Description
Syntax: string.endswith (STR, BEG = [0, End = len (String)])
      String [beg: end] .endswith (str )

parameter Description:
string: - the detected character string
str: - the specified character or substring (tuples may be used, one by one match)
BEG: - set string detecting the starting position (optional, starting from the left)
end: - detection of the end position of the string set (optional, starting from the left)
if the parameter is present and beg end, it is checked in the specified range, otherwise check the whole string  
 
return value:
If the string is detected, the process returns True, otherwise it returns False.

Analysis: If the string is a string str end, returns True, otherwise False

Note: The null character would think that is true

python
>>> endsql = 'select * from ifrs.indiv_info'
>>> endsql.endswith('info')
True
>>> endsql.endswith('info',3)
True
>>>
>>> endsql.endswith('info',3,10)
False
>>> endsql.endswith('info',25,29)
True
>>> endsql.endswith('')
True


Common environment: to determine the file type (such as images, executable files)

>>> f = 'a.txt'
>>> if f.endswith(('.txt')):
...  print '%s is a txt' %f
... else:
...  print '%s is not a txt' %f
...
a.txt is a txt

 

Guess you like

Origin www.cnblogs.com/hello-wei/p/11390518.html