isalpha()函数python怎么用,实例代码

isalpha()函数描述

isalpha()在python中是字符串的方法,可以用dir(str)函数的返回值中来验证这一点。isalpha()方法不接收参数,通过str.isalpha()的方式进行引用,判断字符串的构成是否都是由字母构成,返回值为布尔类型值。


isalpha()方法参数

一般情况下不接收参数,通过str.isalpha()的方式进行类实例方法的引用;

isalpha()方法返回值

True or False


isalpha()方法的实例代码

>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> 'abc'.isalpha()
True
>>> '1a'.isalpha()
False

代码解析

如上代码,通过dir(str)函数的返回值,可以发现,str中确实存在isalpha()的方法,如果是电脑端的话,应该是在第六行。isalpha()的使用方法是通过python的str字符串对象进行引用,当字符串中都是字母的时候,返回True,否则返回False。



全栈后端 / python教程 :


























Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.