[Python] string.find() aways evaluates to true

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have a small script that reads a file. After reading a line I'm trying to figure out if the particular line has a specific text in it. I do so like this:

    for line in file:
    line = line.lower()

    if line.find('my string'):
    print ('found my string in the file')


    I find that line.find always evaluates to true. When I do this

    for line in file:
    line = line.lower()

    if 'one big line'.find('my string'):
    print ('found my string in the file')


    it evaluates to false, as it's supposed to do.

    Continue reading...

Compartilhe esta Página