Detailed explanation of prefix and suffix operations in python

黄舟
Release: 2017-10-07 11:40:58
Original
2046 people have browsed it


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])
Copy after login
.txt
<type &#39;str&#39;>
Copy after login

The bool judgment of the prefix name

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

The bool judgment of the suffix name

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

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;))
Copy after login
('first_directory/second_directory/file', '.txt')
.txt
<type &#39;str&#39;>

True
False


True
False
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template