1、 startswith(): 判断字符串是否以某个子串开始,是则返回True,否则返回False
示例:

my_str = 'hello world and my and test and python'
# 1、 startswith(): 判断字符串是否以某个子串开始,是则返回True,否则返回False
print(my_str.startswith('hello'))  # True
print(my_str.startswith('hel'))  # True
print(my_str.startswith('hells'))  # False

结果:

2、endswith(): 判断字符串是否以某个子串结束,是则返回True, 否则返回False
示例:

my_str = 'hello world and my and test and python'
# 2、endswith(): 判断字符串是否以某个子串结束,是则返回True, 否则返回False
print(my_str.endswith('python'))    # True
print(my_str.endswith('py'))        # False
print(my_str.endswith('on'))        # True

结果:

3、isalpha():判断非空字符串是不是都是字母,是则返回True,否则返回False
示例:

# isalpha():判断非空字符串是不是都是字母,是则返回True,否则返回False
my_str1 = 'my name python'
my_str2 = 'python'
print('isalpha():判断非空字符串是不是都是字母,是则返回True,否则返回False')
print(my_str1.isalpha())    # False
print(my_str2.isalpha())    # True

结果:

4、isdigit():判断非空字符串是不是都是数字,是则返回True,否则返回False
示例:

# isdigit():判断非空字符串是不是都是数字,是则返回True,否则返回False
my_str1 = 'my name python'
my_str3 = '123'
print('isdigit():判断非空字符串是不是都是数字,是则返回True,否则返回False')
print(my_str1.isdigit())    # False
print(my_str3.isdigit())    # True

结果:

5、isalnum():判断非空字符串是不是数字或字母或数字与字母的组合
示例:

# isalnum():判断非空字符串是不是数字或字母或数字与字母的组合
my_str1 = 'my name python'
my_str2 = 'python'
my_str3 = '123'
my_str4 = '123abc'
print('isalnum():判断非空字符串是不是数字或字母或数字与字母的组合')
print(my_str1.isalnum())    # False
print(my_str2.isalnum())    # True
print(my_str3.isalnum())    # True
print(my_str4.isalnum())    # True

结果:

6、isspace():判断字符串是不是空白
示例:

# isspace():判断字符串是不是空白
my_str1 = 'my name python'
my_str5 = ''
my_str6 = ' '
print('isspace():判断字符串是不是空白')
print(my_str1.isspace())    # False
print(my_str5.isspace())    # False
print(my_str6.isspace())    # True

结果:

欢迎大家多多关注我的微信公众号,每日更新Python知识分享

本文地址:https://blog.csdn.net/weixin_44239872/article/details/110443470