Python字符串(三):字符串的判断

    xiaoxiao2025-10-08  2

    1.字符串的判断

    isalnum判断字符串是否完全由字母或数字组成isalpha判断字符串是否完全由字母组成isdigit判断字符串是否完全由数字组成isupper判断字符串当中的字母是否完全是大写islower判断字符串中的字母是否完全是小写istitle判断字符串是否满足title格式isspace判断字符串是否完全由空格组成startswith判断字符串的开头字符,也可以截取判断endswith判断字符串的结尾字符,也可以截取判断split判断字符串的分隔符切片

    练习:isalnum()

    print('hello123'.isalnum()) print('hello123@'.isalnum()) 运行结果:True False

    练习:isalpha()

    print('hello12'.isalpha()) print('hello'.isalpha()) 运行结果:False True

    练习:isdigit()

    print('hello12'.isdigit()) print('123'.isdigit()) 运行结果:False True

    练习:isupper()

    print('HE'.isupper()) print('hL'.isupper()) 运行结果:True False

    练习:isspace()

    print(' '.isspace()) print('h'.isspace()) 运行结果:True False

    练习:startswith()、endswith()

    print('hello'.startswith('h')) #判断是否以‘h’开头 print('hello'.startswith('l')) print('hello'.endswith('h')) #判断是否以‘h’结尾 print('hello'.endswith('o')) 运行结果:True False False True

    练习:split()

    print('hello world'.split()) print('hello'.split()) print('hello'.split('h')) 运行结果: ['hello', 'world'] ['hello'] ['', 'ello']
    最新回复(0)