栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python @ 小甲鱼网课】 P8字符串(3)判断

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python @ 小甲鱼网课】 P8字符串(3)判断

字符串(3)
  • 判断
    • startswith()
      • startswith("a")
      • startswith("a",start,end)
      • startswith(("a","b","c"),start,end) 元组
    • endswith()
    • istitle()
    • isupper()
    • islower()
    • isalpha()
    • isspace()
    • isprintable()
    • isdecimal & isdigit & isnumeric
    • isalnum()
    • isidentifier()
  • 判断字符是否为python保留标识符(if while for ...)

判断
函数含义
startswith()判断指定字符、字符串、元组是否出现在字符串的开始位置
endswith()判断指定字符、字符串、元组是否出现在字符串的结束位置
istitle()判断是否字符串中每个开头都为大写,其他为小写
isupper()判断是否都为大写
islower()判断是否都为小写
isalpha()判断是否都为字母
isspace()判断是否为空白字符串
isprintable()判断是否存在转义字符
isdecimal()判断是否为数字
isdigit()判断是否为数字
isnumeris()判断是否为数字
isalnum()判断字符串是否由字母和数字构成
isidentifier()判断字符串是否为合法字符串
startswith() startswith(“a”)
x = "hello world"
print(x.startswith("h"))
print(x.startswith("H"))
print(x.startswith("hello"))

startswith(“a”,start,end)
x = "hello world"
print(x.startswith("e",1))
print(x.startswith("ello",1))

startswith((“a”,“b”,“c”),start,end) 元组

startswith不仅仅可以判断字符、字符串,也可以判断元组

x = "hello world"
if x.startswith(("a","b","h")):
    print("yes")
else:
    print("no")


endswith()

类比startswith,参数相同,只是一个判断开头一个判断结尾。

endswith(“a”) & endswith(“a”,start,end)

x = "hello world"
print(x.endswith("d"))
print(x.endswith("world"))
print(x.endswith("worl",0,-1))
print(x[-2])


istitle()

判断字符串是否为大小字母开头,其他字符为小写字符

x = "Hello World"
print(x.istitle())
x = "HEllo World"
print(x.istitle())
x = "Hello world"
print(x.istitle())


isupper()

判断字符串中是否所有字符都是大写

x = "Hello World"
print(x.isupper())
x = "HELLO WORLD"
print(x.isupper())
x = "HELLO WORLd"
print(x.isupper())


islower()

类比isupper(),islower()判断是否字符串中所有字符都为小写字符

x = "hello world"
print(x.islower())
x = "hello World"
print(x.islower())
x = "HELLO world"
print(x.islower())


isalpha()

判断字符串中是不是都是字母

x = "hello world"
print(x.isalpha())
x = "helloworld"
print(x.isalpha())
x = "hello1111"
print(x.isalpha())


第一个是 Flase 的原因是 hello 和 world 中间出现空格非字母


isspace()

判断字符串是否为空白字符串

x = "     nt"
print(x.isspace())


isprintable()

判断是否可以打印,主要是判断是否存在转义字符

x = "hello world"
print(x.isprintable())
x = "hello worldn"
print(x.isprintable())


isdecimal & isdigit & isnumeric

判断是否为数字,但是可以判断的范围不同

x = "12345"
print(x.isdecimal())
print(x.isdigit())
print(x.isnumeric())
x = "2²"
print(x.isdecimal())
print(x.isdigit())
print(x.isnumeric())
x = "Ⅰ"
print(x.isdecimal())
print(x.isdigit())
print(x.isnumeric())
x = "一二三四五"
print(x.isdecimal())
print(x.isdigit())
print(x.isnumeric())


isalnum()

集大成者,只要isalpha、isdecimal、isdigit、isnumeric中有一个为true则为true


isidentifier()

判断字符串是否为合法的标识符
合法:不可以数字开头,不存在空格…

x = "hello world"
print(x.isidentifier())
x = "hello_world"
print(x.isidentifier())
x = "520hello"
print(x.isidentifier())


判断字符是否为python保留标识符(if while for …)
import keyword
print(keyword.iskeyword("if"))
print(keyword.iskeyword("while"))
print(keyword.iskeyword("array"))

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/876742.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号