Home  >  Article  >  Backend Development  >  Python intercepts and takes out part of the string

Python intercepts and takes out part of the string

高洛峰
高洛峰Original
2017-03-01 11:54:153983browse

The following is obtained by split interception

>>> str = 'http://manualfile.s3.amazonaws.com/pdf/gti-chis-1-user-9fb-0-7a05a56f0b91.pdf'
>>> print str.split()
['http://manualfile.s3.amazonaws.com/pdf/gti-chis-1-user-9fb-0-7a05a56f0b91.pdf']
>>> print str.split('/')
['http:', '', 'manualfile.s3.amazonaws.com', 'pdf', 'gti-chis-1-user-9fb-0-7a05a56f0b91.pdf']
>>> print str.split('/')[-1]
gti-chis-1-user-9fb-0-7a05a56f0b91.pdf
>>> print str.split('/')[-1].split('.')[0]
gti-chis-1-user-9fb-0-7a05a56f0b91
>>>

The following is obtained by slicing

name = str[str.rfind("/")+1:str.rfind(".")]

The result is the same as above.

If we find other methods, we will continue to add them.

The above python interception method to extract part of the string is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more python interception and taking out part of the string related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn