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

yipeiwu_com6年前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中的random()方法的使用介绍

 random()方法返回一个随机浮点数r,使得0是小于或等于r 以及r小于1。 语法 以下是random()方法的语法: random ( ) 注意:此函数是无法直...

Python matplotlib画图实例之绘制拥有彩条的图表

Python matplotlib画图实例之绘制拥有彩条的图表

生产定制一个彩条标签。 首先导入: import matplotlib.pyplot as plt import numpy as np from matplotlib import...

python实现图片转字符小工具

python实现图片转字符小工具

本文实例为大家分享了python图片转字符小工具的具体实现代码,供大家参考,具体内容如下 from PIL import Image #灰度与字符的映射 ascii_char =...

详解python的几种标准输出重定向方式

一. 背景 在Python中,文件对象sys.stdin、sys.stdout和sys.stderr分别对应解释器的标准输入、标准输出和标准出错流。在程序启动时,这些对象的初值由sys...

Python实现返回数组中第i小元素的方法示例

Python实现返回数组中第i小元素的方法示例

本文实例讲述了Python实现返回数组中第i小元素的方法。分享给大家供大家参考,具体如下: #! /usr/bin/env python #coding=utf-8 #期望为线性时间...