1. isalnum()

语法:

str.isalnum()

返回值:

如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False

str1 = 'this2018'
str2 = 'hello world!'
print(str1.isalnum())
print(str2.isalnum())实例输出结果为:True
False

2. isalpha()

语法:

str.isalpha()

返回值:

如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False

str1 = 'hello'
str2 = 'hello world!' #有空格和感叹号
print(str1.isalpha())
print(str2.isalpha())实例输出结果为:True
False

3. isdigit()

语法:

str.isdigit()

返回值:

如果字符串只包含数字则返回 True 否则返回 False

str1 = '0123456789'
str2 = 'hello2018'
print(str1.isdigit())
print(str2.isdigit())实例输出结果为:True
False

4. islower()

语法:

str.islower()

返回值:

如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False

str1 = 'hello0123456789'
str2 = 'Helloworld'
print(str1.islower())
print(str2.islower())实例输出结果为:True
False

 5. isnumeric()

语法:

str.isnumeric()

返回值:

如果字符串中只包含数字字符,则返回 True,否则返回 False

注:这种方法是只针对unicode对象。

str1 = u'0123456789'
str2 = u'hello0123456789'
print(str1.isnumeric())
print(str2.isnumeric())实例输出结果为:True
False

6. isspace()

语法:

str.isspace()

返回值:

如果字符串中只包含空格,则返回 True,否则返回 False

str1 = '    '
str2 = 'hello world'
print(str1.isspace())
print(str2.isspace())实例输出结果为:True
False

7. istitle()

语法:

str.istitle()

返回值:

如果字符串中所有的单词拼写首字母为大写,且其他字母为小写则返回 True,否则返回 False

str1 = 'My Heart Will Go On'
str2 = 'My heart Will go on'
print(str1.istitle())
print(str2.istitle())实例输出结果为:True
False

8. isupper()

语法:

str.isupper()

返回值:

如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False

str1 = 'HELLO0123456789'
str2 = 'Helloworld'
print(str1.isupper())
print(str2.isupper())实例输出结果为:True
False

【Python】检测字符串的方法相关推荐

  1. python的字符串一些方法

    python的字符串一些方法 python的字符串内建函数 字符串方法是从python1.6到2.0慢慢加进来的--它们也被加到了Jython中. 这些方法实现了string模块的大部分方法,如下表所 ...

  2. Python 3 字符串 format( ) 方法

    Python 3 字符串 format( ) 方法 Python 3.8.5 语法格式: str.format(*args, **kwargs) 描述: 执行字符串格式化操作. Python 2.6 ...

  3. Python 3 字符串 maketrans( ) 方法

    Python 3 字符串 maketrans( ) 方法 Python 3.8.5 语法格式: static str.maketrans(x[, y[, z]]) 描述: 此静态方法返回一个可供 st ...

  4. Python 3 字符串 encode( ) 方法

    Python 3 字符串 encode( ) 方法 Python 3.8.5 语法格式: str.encode(encoding= 'UTF-8', errors= 'strict') 描述: enc ...

  5. Python 3 字符串 center( ) 方法

    Python 3 字符串 center( ) 方法 Python 3.8.5 语法格式: str.center(width[, fillchar]) ​描述: 返回一个指定宽度(width)和填充字符 ...

  6. Python 3 字符串 strip( ) 方法

    Python 3 字符串 strip( ) 方法 Python 3.8.5 语法格式: str.strip([chars]) 描述: 移除字符串首尾指定的字符或字符序列,默认为空格. 移除规则:最外侧 ...

  7. Python 3 字符串 ljust( ) 方法

    Python 3 字符串 ljust( ) 方法 Python 3.8.5 语法格式: str.ljust(width[, fillchar]) 描述: 返回宽度为 width 的字符串,原字符串在其 ...

  8. Python 3 字符串 rstrip( ) 方法(转载)

    Python 3 字符串 rstrip( ) 方法 Python 3.8.5 语法格式: str.rstrip([chars]) 描述: 移除字符串尾部的空格或指定字符串. 移除规则:尾部的字符的移除 ...

  9. Python 检测字符串开始值String.StartsWith 方法

    startsWith() 方法用于检测字符串是否以指定的前缀开始. 语法 public boolean startsWith(String prefix, int toffset)或public bo ...

  10. python反转字符串(简单方法)及简单的文件操作示例

    Python反转字符串的最简单方法是用切片: >>> a='123456' >>> print a[::-1] 654321 切片介绍:切片操作符中的第一个数(冒号 ...

最新文章

  1. 为什么使用HashMap需要重写hashcode和equals方法_java常见面试题敲黑板了,HashMap最全的整理,大厂必考...
  2. phpstorm—下载安装配置教程
  3. SpringBatch之CompositeItemWriter详解
  4. 用jedis访问Redis进行对象存取示例
  5. VTK:定向圆柱体用法实战
  6. HDU多校3 - 6797 Tokitsukaze and Rescue(dfs+最短路)
  7. 控制台一直报错, [WDS] Disconnected!
  8. 人工智能是互联网下一轮变革的核心
  9. 强制修改上网本分辨率
  10. 【LeetCode笔记】35. 搜索插入位置(Java、二分法)
  11. LeetCode OJ 之 Valid Anagram
  12. 【报告分享】2019区块链赋能新型智慧城市白皮书.pdf(附204页电子书下载链接)
  13. ROS2——南溪的ROS2使用手册
  14. 傅盛希望定义AI时代的机器人产品,2年交出这份可落地答卷
  15. 小程序切换账户拉取仓库文件的appid提示
  16. CrazyWing:Python自动化运维开发实战 十八、Python面向对象
  17. linux小红伞安装黑屏,在linux下安装Avria(小红伞)
  18. Weblogic 部署问题及解决方法
  19. 《德鲁克管理思想精要》读书笔记10 - 沟通,领导力,创新的原则
  20. Unity的五个窗口面板

热门文章

  1. 使用Android OpenGL ES 2.0绘图之五:添加运动
  2. WIZnet 在 Embedded World 2013展会中的展示
  3. Eclipse用法和技巧六:自动生成get和set方法1
  4. hadoop集群配置SSH免登陆
  5. Android高级-Android操作SQL数据管理,增删改查
  6. JAVA 8:Lambdas表达式初体验
  7. Apache CXF实现Web Service(5)—— GZIP使用
  8. C语言及程序设计进阶例程-30 联合体及其应用
  9. 在 Linux 下搭建 Java 开发环境
  10. 配置IIS支持PHP