python text string start or end match
Scene:
the beginning or end of a string matches, typically using a matching file type or URL
General use Startwith or Endwith
>>>a=' http://blog.csdn.net/raylee2007 '
>>>A.startswith (' http ')
True
Note: The parameters inside these two methods can be Str , or it can be a tuple, but not a list or a dictionary
>>>a=' http://blog.csdn.net/raylee2007 '
>>>A.startswith ((' http ',' FTP '))
True
If it is a list or dictionary, the error
>>>a=' http://blog.csdn.net/raylee2007 '
>>>A.startswith ([' http ',' FTP '])
Traceback (most recent):
File "", Line1, inch
A.startswith ([' http ',' FTP '])
TypeError:StartsWith First arg must be strora tuple of STR, notList
>>>
In fact, in addition to the above method, You can also use slices to do it, but the code doesn't look that good.
>>>a=' http://blog.csdn.net/raylee2007 '
>>>a[0:4]==' http '
True
>>>
Of course, we can do it with regular expressions, but it's a bit more difficult to understand.
>>>Import re
>>>URL ='http://www.python.org'
>>>Re.match (' http:|https:|ftp: ', URL)
0, 5), match=' http: '>
>>>Help (Re.match)
Help on function matchinch Module Re:
Match (pattern, string, flags=0)
Try To apply the pattern at the start of the string, returning
A match object,orNoneifNo match was found.
>>>
Python text string beginning or ending match