user1977050
user1977050

Reputation: 526

python - convert string to int with space delimiter

I'm using some library which converts data to other format. As I don't want to debug this library, I'd like to solve the solution on my code.

The input is integer and maybe -1. In this case and the library output is - 1. I need to compare this integer if this is positive or negative. How can I handle the - 1 for this check?

Upvotes: 0

Views: 152

Answers (2)

ihdv
ihdv

Reputation: 2277

If you have a string x="- 1" and want to convert it to int, eval(x) works.

Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = "- 1"
>>> intx = eval(x)
>>> print(type(intx))
<class 'int'>
>>> print(intx)
-1

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 113948

int("- 1") # nvm this doesnt work in py3

i think works (but not float('- 1') )

you could also just remove all the spaces

float("- 1".replace(" ",""))

Upvotes: 0

Related Questions