python中常用检测字符串相关函数汇总

yipeiwu_com5年前Python基础

本文实例汇总了python中常用检测字符串相关函数。分享给大家供大家参考。具体分析如下:

下面的python代码可用于检测字符串,包括是否全部为数字,是否包含数字,是否包含标题单词,是否包含大写字母,是否包含小写字母,是否包含空格,是否以指定的字符开头和结尾。

my_string = "Hello World"
my_string.isalnum()   #检测所有字符是否都是数字
my_string.isalpha()   #检测字符串中的所有字符是否都是字母
my_string.isdigit()   #检测字符串是否包含数字
my_string.istitle()   #检测字符串是否包含标题单词
my_string.isupper()   #检测字符串是否包含大写字母
my_string.islower()   #检测字符串是否包含小写字母
my_string.isspace()   #检测字符串是否包含空格
my_string.endswith('d')   #检测字符串是否以字符d结束
my_string.startswith('H')  #检测字符串是否以大写字母H开头

下面显示返回结果

my_string="Hello World"
print my_string.isalnum()    #False
print my_string.isalpha()    #False
print my_string.isdigit()    #False
print my_string.istitle()    #True
print my_string.isupper()    #False
print my_string.islower()    #False
print my_string.isspace()    #False
print my_string.endswith('d')    #True
print my_string.startswith('H')   #True

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python使用pip安装报错:is not a supported wheel on this platform的解决方法

Python使用pip安装报错:is not a supported wheel on this platform的解决方法

本文讲述了Python使用pip安装报错:is not a supported wheel on this platform的解决方法。分享给大家供大家参考,具体如下: 可能的原因1:安...

wxPython中文教程入门实例

wxPython中文教程入门实例 wx.Window 是一个基类,许多构件从它继承。包括 wx.Frame 构件。可以在所有的子类中使用 wx.Window 的方法。 wxPython的...

Python Pandas中根据列的值选取多行数据

Pandas中根据列的值选取多行数据 # 选取等于某些值的行记录 用 == df.loc[df['column_name'] == some_value] # 选取某列是否是某...

python rsa 加密解密

最近有需求,需要研究一下RSA加密解密安全;在网上百度了一下例子文章,很少有文章介绍怎么保存、传输、打印加密后的文本信息,都是千篇一律的。直接在一个脚本,加密后的文本信息赋于变量,然后立...

利用rest framework搭建Django API过程解析

利用rest framework搭建Django API过程解析

思路步骤: 创建一个可以序列化的类 去数据库取数据交给序列化的类处理 把序列化的数据返回前端 操作流程: # 安装模块 pip install djangorestfra...