国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python startswith()和endswith() 方法原理解析

瀏覽:85日期:2022-07-27 09:41:21

startswith()方法

Python startswith() 方法用于檢查字符串是否是以指定子字符串開頭

如果是則返回 True,否則返回 False。如果參數 beg 和 end 指定值,則在指定范圍內檢查。

str.startswith(str, beg=0,end=len(string));

參數

str --檢測的字符串。 strbeg --可選參數用于設置字符串檢測的起始位置。 strend --可選參數用于設置字符串檢測的結束位置。

返回值

如果檢測到字符串則返回True,否則返回False。

常用環境:用于IF判斷

#!/usr/local/bin/python# coding=utf-8listsql = ’select * from ifrs.indiv_info’def isSelect(sql): chsql = sql.upper().strip() if not chsql.startswith('SELECT '): return False return Trueprint isSelect(listsql)[root@bigdata-poc-shtz-3 zw]# python h.pyTrue

endswith()方法

作用:判斷字符串是否以指定字符或子字符串結尾,常用于判斷文件類型

一、函數說明

語法:string.endswith(str, beg=[0,end=len(string)])

string[beg:end].endswith(str)

參數說明:

string: --被檢測的字符串 str: --指定的字符或者子字符串(可以使用元組,會逐一匹配) beg: --設置字符串檢測的起始位置(可選,從左數起) end: --設置字符串檢測的結束位置(可選,從左數起)

如果存在參數 beg 和 end,則在指定范圍內檢查,否則在整個字符串中檢查

返回值:

如果檢測到字符串,則返回True,否則返回False。

解析:如果字符串string是以str結束,則返回True,否則返回False

注:會認為空字符為真

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

常用環境:用于判斷文件類型(比如圖片,可執行文件)

>>> 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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 灌阳县| 宁波市| 武定县| 南昌市| 吉水县| 隆子县| 缙云县| 长顺县| 贡山| 潼南县| 建宁县| 祁连县| 巧家县| 灵石县| 黔西县| 格尔木市| 修文县| 固始县| 淳安县| 新化县| 通渭县| 徐汇区| 铁岭市| 饶河县| 平定县| 兴城市| 岳阳市| 文昌市| 密山市| 新兴县| 宁晋县| 鹰潭市| 葵青区| 阜平县| 冕宁县| 荃湾区| 深州市| 铁力市| 崇州市| 简阳市| 北辰区|