Is it possible to add int with str composed of text and numbers in Python?

Asked

Viewed 2,559 times

3

I asked that question Why in PHP the expression "2 + '6 apples'" is 8? here for finding this behavior strange.

And in the case of python?

I can convert the string '1' str for 1 int.

Example:

1 + int('1') # Imprime: 2

But if I try to convert '1 cachorro' str for int ...

int('1 cachorro');

... an error is generated:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1 cachorro'

It would be possible to make expressions of the type 1 + int('1 cachorro') function in python?

If I needed to do this (I think I’ll never need to), how could I treat this case?

  • Wallace Maxter’s doubt turned out to be for me too, but I found the solution here: http://answall.com/questions/42280/como-extrair-d%C3%Adgitos-de-uma-string-em-python-e-som%C3%A1-los-entre-si

3 answers

4


It would be possible to make expressions of the type 1 + int('1 cachorro') work in python?

Not. Or you sum whole or concatenate strings. This is part of Python type security.

If I needed to do this (I think I’ll never need to), how could I treat this case?

The only reasonable operation would be the concatenation of strings. If you want to add an integer to a string in places where Python has located numbers - which would be the most exotic interpretation of your question -, you would have to write your own function to accomplish this, and would not be using the sum operator.

  • madness of PHP vs sanity python! Which to choose?

  • 3

    @Wallacemaxters And still you ask.

  • In PHP you just can’t add one int with array. If it doesn’t generate a fatal Error (and many innocent people fall for it, without treating the data coming from the url)!

3

Your question already has one reply in the English version of Stackoverflow!

The operator '+' works like this:

  • If the 2 operands are numbers: add

  • Else: concatenates strings.

If the string contains only numbers it can convert to integer.. otherwise gives an Exception.

Therefore, in the code where you want to convert a string to int you must "wait" for an exception to happen.

string = "abcd"
try:
    i = int(string)
    print i
except ValueError:
    #Trata a exceção
    print 'a string não é um número'
  • I’m sorry, Pedro, but I’m still not very good at English.

  • I’ll put what’s important in the answer

  • Now, yes. When I saw Link, I was a little lost in which of the SOEN responses you were pointing to. Tip: Always click on "share" in the answer, because the generated link will fall straight in the answer of the question :)

  • thanks for the tip. I edited the link !

0

To do what you want, I think you can do the following:

int( re.search(r'\d+', '1cachorro).group() )

What this will do is take the string you placed, search the integer and return it. Then you can use this value to add it up.

Browser other questions tagged

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