为什么检查空字符串字符串.十六进制数字()返回有效?

2024-04-20 06:21:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个函数,一个用于清除输入文本,另一个用于测试清除文本是否为十六进制。当向cleaner函数传递一个包含所有空格的字符串时,它会去掉这些空格,留下一个空字符串。你知道吗

我不明白为什么字符串.十六进制数字()不会在这个空字符串上呕吐,让它作为有效的十六进制传递。你知道吗

def testHex(ciphertext):
    cleancipher = cleanHex(ciphertext)
    if all(h in string.hexdigits for h in cleancipher):
        print('String is valid hex.')
    else:
        print('String is not valid hex.')

def cleanHex(ciphertext):
    return(ciphertext.replace(' ', '').replace('0x', '').replace(':', '').replace('\\x', '').strip())

Tags: 函数字符串in文本stringisdefreplace
1条回答
网友
1楼 · 发布于 2024-04-20 06:21:56

testHex为空字符串输出String is valid hex.,因为all为空iterables返回True

它是documented

all(iterable)

Return True if all elements of the iterable are true (or if the iterable is empty).

相关问题 更多 >