Home  >  Article  >  Backend Development  >  Detailed explanation of prefix and suffix operations in python

Detailed explanation of prefix and suffix operations in python

黄舟
黄舟Original
2017-10-07 11:40:582033browse


Return the suffix name

import os

path = 'first_directory/second_directory/file.txt'print os.path.splitext(path)[1]print type(os.path.splitext(path)[1])
.txt
<type &#39;str&#39;>

The bool judgment of the prefix name

path = &#39;first_directory/second_directory/file.txt&#39;print path.startswith(&#39;fir&#39;)
True

The bool judgment of the suffix name

path = &#39;first_directory/second_directory/file.txt&#39;print path.endswith(&#39;.txt&#39;)
True

os.path. splitext, .startswith, .startswith explore

import os

path = &#39;first_directory/second_directory/file.txt&#39;print os.path.splitext(path)print os.path.splitext(path)[1]print type(os.path.splitext(path)[1])printprint path.startswith(&#39;fir&#39;)print path.startswith(&#39;aaa&#39;)print type(path.startswith(&#39;fir&#39;))printprint path.endswith(&#39;.txt&#39;)print path.endswith(&#39;.aaa&#39;)print type(path.endswith(&#39;.txt&#39;))
('first_directory/second_directory/file', '.txt')
.txt
<type &#39;str&#39;>

True
False


True
False

The above is the detailed content of Detailed explanation of prefix and suffix operations in python. For more information, please follow other related articles on 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