Finding certain text in a string

-2

Hello, I have a question.

nome=str(input('Qual o seu nome completo?'))
print('Seu nome tem Enzo?{}'.format('Enzo' in nome.lower()))

I have this code that it checks without having a certain string inside the other, the problem is I want it to check without having a certain name inside the text in which I type and do a certain action. Ex: See if it has the word "Enzo" and if you type something on the screen.

    
asked by anonymous 04.05.2018 / 20:05

4 answers

3

One suggestion for regex would be to use the meta-character \b , eg:

import re

nome = input('Qual o seu nome completo? ')

if re.search('\benzo\b', nome, re.IGNORECASE):
    print("A string tem o nome Enzo")
else:
    print("A string não tem o nome Enzo")

\b is a meta-character that is used to find a match at the beginning or end of a word, thus avoiding words like:

enzon
fenzo

If you run in:

import re

print( re.search('enzo', 'enzon', re.IGNORECASE) ) # retorna <_sre.SRE_Match object; span=(0, 4), match='enzo'>
print( re.search('enzo', 'fenzo', re.IGNORECASE) ) # retorna <_sre.SRE_Match object; span=(0, 4), match='enzo'>
print( re.search('enzo', 'cheetos', re.IGNORECASE) ) # retorna None

Now using \b note how it works:

import re

print( re.search('\benzo\b', 'enzon', re.IGNORECASE) ) # retorna None
print( re.search('\benzo\b', 'fenzo', re.IGNORECASE) ) # retorna None
print( re.search('\benzo\b', 'cheetos', re.IGNORECASE) ) # retorna None

print( re.search('\benzo\b', 'enzo foo bar', re.IGNORECASE) ) # retorna <_sre.SRE_Match object; span=(0, 4), match='enzo'>
print( re.search('\benzo\b', 'foo enzo bar', re.IGNORECASE) ) # retorna <_sre.SRE_Match object; span=(4, 8), match='enzo'>

The \b meta-character is a real hand on the wheel to avoid such problems.

    
05.05.2018 / 04:01
5

Your code has two important considerations that were not satisfactorily addressed in the other answers.

1) Redundancy in function input . If you read the official function documentation , you will see the following excerpt:

  

The function then reads a line from input, converts it to string (stripping a trailing newline), and returns that.

That is, the function return will always be a string , regardless of the content read. So, str(input(...)) is to insert redundancy into the code and make one ¹ too unnecessary call.

2) Sensitive to the text box. The comparison you made was 'Enzo' in nome.lower() and it will never be valid, since you are basically looking for a string with uppercase characters in a string only by lowercase, since you used the lower() method. It would be the same as searching for the number 7 in a set of even numbers. Logic is right, but implementation failed.

That said, your code would basically be:

nome = input('Qual é o seu nome completo?')
print('Seu nome tem Enzo? {}'.format('Enzo' in nome))

See working at Repl.it | Ideone | GitHub GIST

However, this would produce output such as:

Seu nome tem Enzo? True
Seu nome tem Enzo? False

It's strange to have True or False in the middle of a string in Portuguese. At least I think. It would make more sense for me to appear "yes" or "no", and for that, it would be enough to do:

nome = input('Qual é o seu nome completo?')
tem_enzo = 'Sim' if 'Enzo' in nome else 'Não'

print(f'Seu nome tem Enzo? {tem_enzo}')

See working at Repl.it | Ideone | GitHub GIST

To make the check that is not case-sensitive, you only need to use the lower method, but be careful to search for a string composed only of lowercase characters, such as

'enzo' in nome.lower()
  

Note: Note that this method may not be fully efficient, since it would return as true if the user name was Lorenzo, since it has 'enzo' in the name. If it is interest to check only for the full term, the solution would be another.

¹: It is not just a call, since str is a native Python class, not a function. What happens in this case is the call of the constructor.

    
04.05.2018 / 23:52
2

You can use if for this, it would look something like this

nome=str(input('Qual o seu nome completo?')) 

if("Enzo" in nome):
    print("A string tem o nome Enzo")
else:
    print("A string não tem o nome Enzo")

This way above will always be case sensitive , to ignore this has this other option

import re

nome=str(input('Qual o seu nome completo? ')) 

if re.search('enzo', nome, re.IGNORECASE):
    print("A string tem o nome Enzo")
else:
    print("A string não tem o nome Enzo")
    
04.05.2018 / 20:10
1

Hello,

To check occurrences in a string, you can use count (), which counts how many times a given substring is found in a string. See:

nome=input("Qual o seu nome completo?") 

if(nome.count("Enzo")):
    print("A string tem o nome Enzo")
else:
    print("A string não tem o nome Enzo")
    
04.05.2018 / 20:16