在if语句中使用字符串

Eja*_*jaz 3 python if-statement python-3.x

任何人都可以解释在以下代码word的语句if word:中正在执行哪些检查?

def simplify(text, space=" \t\n\r\f", delete=""):
    result = []
    word = ""
    for char in text:
        if char in delete:
            continue
        elif char in space:
            if word:
                result.append(word)
                word = ""
        else:
            word += char
    if word:
        result.append(word)
    return " ".join(result)
Run Code Online (Sandbox Code Playgroud)

Zek*_*oid 7

python中的非空字符串始终为True,否则为False.因此,如果word仍为空字符串,则为False,否则为True.