isalnum_Python字符串| isalnum()和isalpha()方法以及示例

isalnum

1)isalpha()方法 (1 ) isalpha() Method)

isalpha() method returns true if all characters of a string are only alphabets (between ‘A’ to ‘Z’ and between ‘a’ to ‘z’). If there is any non alphabet character in the string, method returns false.

如果字符串的所有字符都是字母(“ A”至“ Z”之间以及“ a”至“ z”之间),则isalpha()方法返回true 。 如果字符串中有任何非字母字符,则方法返回false 。

Syntax:

句法:

    string.isalpha()

Parameter: None

参数:

Return type:

返回类型:

  • true - if all characters of the string are alphabets.

    true-如果字符串的所有字符都是字母。

  • false - if any of the character of the string is not an alphabet (like number, space, special characters).

    false-如果字符串中的任何字符都不是字母(例如数字,空格,特殊字符)。

Example:

例:

# str1 with only alphabets
str1 = "Helloworld"

# str2 with alphabets, space and special character
str2 = "Hello world!"

# str3 with alphabets, space and numbers
str3 = "Hello world 100"

# check whether string contains only alphabets or not
print str1.isalpha ()
print str2.isalpha ()
print str3.isalpha ()

Output

输出量

    True
    False
    False

2)isalnum()方法 (2) isalnum() Method)

isalnum() method returns true if all characters of a string are either alphabets (between ‘A’ to ‘Z’ and between ‘a’ to ‘z’) or numbers (0 to 9) or both, otherwise method returns false.

如果字符串的所有字符都是字母(介于“ A”至“ Z”之间,介于“ a”至“ z”之间)或数字(0至9)或两者皆有,则isalnum()方法返回true ,否则方法返回false 。

Syntax:

句法:

    string.isalnum()

Parameter: None

参数:

Return type:

返回类型:

  • true - if all characters of the string are alphabets or/and numbers.

    true-如果字符串的所有字符都是字母或数字。

  • false - if any of the character of the string is not either an alphabet or a number.

    false-如果字符串中的任何字符都不是字母或数字。

Example:

例:

# str1 with alphabets and numbers
str1 = "Helloworld123"

# str2 with only alphabets
str2 = "Helloworld"

# str3 with numbers only
str3 = "12345"

# str4 with alpa numeric characters and space 
str4 = "Amit Shukla 21"

# check whether string contains only alphabets or not
print str1.isalnum ()
print str2.isalnum ()
print str3.isalnum ()
print str4.isalnum ()

Output

输出量

    True
    True
    True
    False


翻译自: https://www.includehelp.com/python/string-isalnum-and-isalpha-methods-with-examples.aspx

isalnum

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值