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
– GlaucioFonseca