gpt4 book ai didi

python - 在 Python 中,如何检查字符串是否包含字母或数字?

转载 作者:太空狗 更新时间:2023-10-30 01:41:44 25 4
gpt4 key购买 nike

如果字符串包含字母或数字,则返回 true。否则,返回 false。

我必须这样做,对吗?

return re.match('[A-Z0-9]',thestring)

最佳答案

使用 thestring.isalnum()方法。

>>> '123abc'.isalnum()
True
>>> '123'.isalnum()
True
>>> 'abc'.isalnum()
True
>>> '123#$%abc'.isalnum()
>>> a = '123abc'
>>> (a.isalnum()) and (not a.isalpha()) and (not a.isnumeric())
True
>>>

关于python - 在 Python 中,如何检查字符串是否包含字母或数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com