python版本:3.8.1
该版本下,str函数一共有44个,除去已经在别的文章中介绍过的format(参考链接),这里介绍剩下43个函数的用法。有任何错误欢迎在评论中指出。
- isdigit isalpha isalnum isdecimal isnumeric isupper islower isspace isidentifier isprintable istitle split rsplit splitlines index rindex find rfind title encode replace maketrans translate join lstrip rstrip strip ljust rjust center zfill capitalize casefold endswith startswith count expandtabs partition rpartition upper lower swapcase format_map
isdigit
参数:无
用途:判断字符串是否全部由数字组成
>>> "12".isdigit()
True
>>> "a".isdigit()
False
>>> "".isdigit()
False
isalpha
参数:无
用途:判断字符串是否全部由字母组成
>>> "a".isalpha()
True
>>> "a1".isalpha()
False
>>> "".isalpha()
False
isalnum
参数:无
用途:判断字符串是否全部由字母或数字组成
>>> "a1".isalnum()
True
>>> "a1.".isalnum()
False
>>> "".isalnum()
False
isdecimal
参数:无
用途:判断字符串是否全部由十进制数字组成,和isdigit的区别在于,isdigit可用于byte类型,而isdecimal不可以
>>> b"1".isdigit()
True
>>> "1".isdecimal()
True
>>> b"1".isdecimal()
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
b"1".isdecimal()
AttributeError: 'bytes' object has no attribute 'isdecimal'
>>> "".isdecimal()
False
isnumeric
参数:无
用途:判断字符串是否全部由数字字符组成,和isdigit的区别在于,isdigit只能检测阿拉伯数字,isnumeric还可以检测汉字数字(大小写均可),罗马数字
>>> "1二叁".isnumeric()
True
>>> "1Ⅲ".isnumeric()
True
>>> "".isnumeric()
False
注意:上面例子中罗马数字是Ⅲ不是三个I
isupper
参数:无
用途:判断字符串是否全部由大写字母组成
>>> "ABC".isupper()
True
>>> "ABc".isupper()
False
>>> "".isupper()
False
islower
参数:无
用途:判断字符串是否全部由小写字母组成
>>> "abc".islower()
True
>>> "ABc".islower()
False
>>> "".islower()
False
isspace
参数:无
用途:判断字符串是否全部由空白字符组成
>>> " ".isspace() #一个空格
True
>>> " ".isspace() #一个制表符
True
>>> "\n".isspace() #换行符
True
>>> "\r".isspace() #回车符
True
>>> "".<