Find certain text in a string

Asked

Viewed 26,369 times

-1

Hello I’m having a doubt.

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 you have the word "Enzo" and if you have to write something on the screen.

  • Seems to be the case mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

4 answers

7

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

1) Redundancy in function input. If you read the official documentation of the function, you will see the following passage (adapted):

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

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

2) Responsive to the text box. The comparison you made was 'Enzo' in nome.lower() and it will never be valid, because you are basically seeking a string with uppercase characters in a string which is certainly composed only of small characters, since it used the method lower(). It would be the same as fetching the number 7 in a set of even numbers. The logic is right, but failed to implement.

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 on Repl.it | Ideone | Github GIST

However, this would produce an output such as:

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

It’s strange to have True or False in the midst of a string in English. 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 on Repl.it | Ideone | Github GIST

To make the check that is not sensitive to the box, just use the method lower, but taking care to look for a string composed only of lowercase characters, such as in:

'enzo' in nome.lower()

Note: it is worth remembering that this method may not be entirely efficient, since it would return as true if the user’s name was Lorenzo, because it has 'enzo' in the name. If it is interesting to check only by 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 constructor call.

  • If it is Python2 the input Did you receive a number can make the cast not? Of course if it is really python2 it would be interesting to use raw_input. I know it sounds strange, but there are still users of version 2

  • +1 mainly by the part about the case-sensitive.

3

You can use the if for this, it would look more or less 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")

3


A suggestion for with regex would be to use the meta-character \b, example:

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")

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

enzon
fenzo

Rotate 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 the \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 meta-character \b is a real hand on the wheel to avoid such problems.

1

Hello,

To verify occurrences in a string, you can use Count(), which counts how many times a given substring is found in a string. Behold:

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")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.