I encountered this problem in the matplotlib xticklabels Text attribute. The minus signs for negative numbers are encoded as a "minus":

[โˆ’] is a minus (Unicode 2212).

Minus: [&minus]; a.k.a. [&#8722]; a.k.a. [&#x2212];

https://en.wikipedia.org/wiki/Wikipedia:Hyphens_and_dashes

Python seems to code minuses as 'hyphen-minus', Unicode 002D:

[-] is a hyphen-minus (ASCII keyboard, Unicode 002D)

Hyphen minus: [&#45]; a.k.a. [&#x002D];

Here is an example:

>> import matplotlib.pyplot as plt
>> import re
>> x = [0,1,2,3,4,5]
>> y = [0,1,2,1,2,1]

>> fig,ax = plt.subplots(1)
>> plt.plot(x,y)

.

If we try to get at the xticklabels, if we want to manually edit them, we use:

>> l = ax.get_xticklabels()
>> ticks = [i.get_text() for i in l]
>> print(ticks)

['โˆ’1', '0', '1', '2', '3', '4', '5', '6']

>> ord(ticks[0])

8722

Try to convert it to an integer:

>> l = ax.get_xticklabels()
>> ticks = [int(i.get_text()) for i in l]

ValueError: invalid literal for int() with base 10: 'โˆ’1'

This is the same error as in the question, which was difficult for others to recreate. To fix it, use regular expressions:

ticks = [int(re.sub(u"\u2212", "-", i.get_text())) for i in l]
print(ticks)
print(ticks[0] - 1)

[-1, 0, 1, 2, 3, 4, 5, 6]
-2

>> ord(ticks[0])

45
Answer from AhabTheArab on Stack Overflow
Top answer
1 of 6
10

I encountered this problem in the matplotlib xticklabels Text attribute. The minus signs for negative numbers are encoded as a "minus":

[โˆ’] is a minus (Unicode 2212).

Minus: [&minus]; a.k.a. [&#8722]; a.k.a. [&#x2212];

https://en.wikipedia.org/wiki/Wikipedia:Hyphens_and_dashes

Python seems to code minuses as 'hyphen-minus', Unicode 002D:

[-] is a hyphen-minus (ASCII keyboard, Unicode 002D)

Hyphen minus: [&#45]; a.k.a. [&#x002D];

Here is an example:

>> import matplotlib.pyplot as plt
>> import re
>> x = [0,1,2,3,4,5]
>> y = [0,1,2,1,2,1]

>> fig,ax = plt.subplots(1)
>> plt.plot(x,y)

.

If we try to get at the xticklabels, if we want to manually edit them, we use:

>> l = ax.get_xticklabels()
>> ticks = [i.get_text() for i in l]
>> print(ticks)

['โˆ’1', '0', '1', '2', '3', '4', '5', '6']

>> ord(ticks[0])

8722

Try to convert it to an integer:

>> l = ax.get_xticklabels()
>> ticks = [int(i.get_text()) for i in l]

ValueError: invalid literal for int() with base 10: 'โˆ’1'

This is the same error as in the question, which was difficult for others to recreate. To fix it, use regular expressions:

ticks = [int(re.sub(u"\u2212", "-", i.get_text())) for i in l]
print(ticks)
print(ticks[0] - 1)

[-1, 0, 1, 2, 3, 4, 5, 6]
-2

>> ord(ticks[0])

45
2 of 6
9

Actually, I cannot reproduce your error , it just work in python 2.7.13

>>>int("-5")
>>> -5

and in python 3.4

so, it could be a python version problem , I recommend updating your python version , by reinstalling the newer , from the site , it will replace it perfectly ( packages untouched ) , unless you are using , a special distribution like anaconda ..

for processing your list ( mixed chars and numbers ) use:

try: except: statement.

Discussions

python - When using negative numbers to slice a string, why is 0 is disabled? - Stack Overflow
Let's say I have a string: >>>a = 'akwkwas' >>> >>>a[-3:] 'was' >>>a[-3:None] 'was' >>>a[-3:0] '' Why can't I use 0 as the end of the slice? This is from More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 24, 2017
Handling negative number inputs from the user
I would recommend you use a try instead. answer = input(f"What is {a} + {b}") try: answer = int(answer) except ValueError: print("That's not a number") More on reddit.com
๐ŸŒ r/learnpython
19
4
January 29, 2025
Python string formatting: padding negative numbers - Stack Overflow
I would like to format my integers as strings so that, without the sign, they will be zero-padded to have at least two digits. For example I want 1 -1 10 -10 to be 01 -01 10 -10 Specifically, I ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python, negative numbers string to float - Stack Overflow
This is a very simple issue but I am making it needlessly complex and continue to hit road blocks. I am trying to parse a simple text file which contains point cloud information, x, y, z. It lo... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 6, 2015
Top answer
1 of 7
88

Use lstrip:

question.lstrip("-").isdigit()

Example:

>>>'-6'.lstrip('-')
'6'
>>>'-6'.lstrip('-').isdigit()
True

You can lstrip('+-') if you want to consider +6 a valid digit.

But I wouldn't use isdigit, you can try int(question), it'll throw an exception if the value cannot be represented as int:

try:
    int(question)
except ValueError:
    # not int
2 of 7
28

Use a try/except, if we cannot cast to an int it will set is_dig to False:

try:
    int(question)
    is_dig = True
except ValueError:
    is_dig = False
if is_dig:
  ......

Or make a function:

def is_digit(n):
    try:
        int(n)
        return True
    except ValueError:
        return  False

if is_digit(question):
   ....

Looking at your edit cast to int at the start,checking if the input is a digit and then casting is pointless, do it in one step:

while a < 10: 
     try:
        question = int(input("What is {} {} {} ?".format(n1,op,n2)))
     except ValueError:
        print("Invalid input")
        continue # if we are here we ask user for input again

    ans = opsop
    n1 = random.randint(1,9)
    n2 = random.randint(1,9)
    op = random.choice(list(ops))

    if question ==  ans:
        print ("Well done")
    else:
        print("Wrong answer")
    a += 1

Not sure what Z is doing at all but Z = Z + 0 is the same as not doing anything to Z at all 1 + 0 == 1

Using a function to take the input we can just use range:

def is_digit(n1,op,n2):
    while True:
        try:
            n = int(input("What is {} {} {} ?".format(n1,op,n2)))
            return n
        except ValueError:
            print("Invalid input")


for _ in range(a):
    question = is_digit(n1,op,n2) # will only return a value when we get legal input
    ans = opsop
    n1 = random.randint(1,9)
    n2 = random.randint(1,9)
    op = random.choice(list(ops))

    if question ==  ans:
        print ("Well done")
    else:
        print("Wrong answer")
๐ŸŒ
Quora
quora.com โ€บ What-method-checks-that-the-string-is-a-number-and-includes-negative-in-the-Python-language
What method checks that the string is a number and includes negative in the Python language? - Quora
Answer (1 of 8): The simplest way - use the inbuilt โ€˜int(..)โ€™ function and capture the exception that int(..) raises when it finds something that isnโ€™t an int (it raises a ValueError exception). Use try/except to capture whether a call to int() raises a ValueError.
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python isnumeric negative numbers
Python isnumeric negative numbers - Tutorial - By EyeHunts
March 3, 2023 - Alternatively, you can use a regular expression to check whether a given string represents a negative number or not. import re def is_number(s): return bool(re.match(r'^-?\d+(?:\.\d+)?$', s)) print(is_number('123')) # True print(is_number('-123')) # True print(is_number('A1')) #False ยท One simple way to check if a string is a number or not is to try to convert it to a number using a built-in Python function like int() or float().
Top answer
1 of 5
7

0 is the start of the sequence. Always, unambiguously. Changing its meaning to sometimes be the end would lead to a lot of confusion, especially when using variables for those two values.

Using negative indices is also not a different mode; negative indices are converted to positive indices relative to the length. Changing what element 0 refers to because the other slice input (start or stop) was a negative number makes no sense.

Because 0 always means the first element of the sequence, and there is no spelling for a negative zero, you cannot use 0 to mean the end of the sequence.

You can use None as the stop element to mean this instead, if you need to parameterise your indices:

start = -3
stop = None
result = a[start:stop]

You can also create a slice() object; the same rules apply for how indices are interpreted:

indices = slice(-3, None)
result = a[indices]

In fact, the interpreter translates the slice notation into a slice() object, which is then passed to the object to distinguish from straight-up indexing with a single integer; the a[start:stop] notation translates to type(a).__getitem__(a, slice(start, stop)) whereas a[42] becomes type(a).__getitem__(a, 42).

So by using a slice() object you can record either slicing or single-element indexing with a single variable.

2 of 5
1

It is boring to use negative slice in a loop if there is some chance to slice to 'negative zero', because [:-0] is not interpreted as expected.

But there is a simple way to solve the problem, just convert negative index to positive index by adding the length of the container.

E.g. Negative Slice Loop:

a = np.arange(10)
for i in range(5):
    print(a[5-i:-i])

Answer:

[]
[4 5 6 7 8]
[3 4 5 6 7]
[2 3 4 5 6]
[1 2 3 4 5]

Convert to positive by adding the lenght:

for i in range(5):
    print(a[5-i:len(a)-i])

Get the right answer:

[5 6 7 8 9]
[4 5 6 7 8]
[3 4 5 6 7]
[2 3 4 5 6]
[1 2 3 4 5]
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ handling negative number inputs from the user
r/learnpython on Reddit: Handling negative number inputs from the user
January 29, 2025 -

This is a solution post. I had a problem and none of the solutions I found online were right for me. I eventually figured it out, and so I'm putting my solution here for future learners. Also if my solution is bad, I'll get some feedback. If you think it's obvious, then you're very clever, but no need to go to the trouble of letting me know!

I'm making an arithmetic game for my little one. So it had a line:

answer = int(input(f"What is {a} + {b}?"))

but of course he accidentally typed a letter and crashed the program. I wanted to handle this eventuality so I changed it to:

answer = input(f"What is {a} + {b}")
if answer.isnumeric():
    answer = int(answer)
else:
    print("That's not a number")
    continue

the trouble is I also have subtraction questions and negative numbers! But "-1".isnumeric()==False !!

So I started googling: "isnumeric negative numbers" and "parsing negative numbers" and so on. The solutions I found were quite convoluted, mostly they seemed to be worrying about SQL injection and used concepts I hadn't learned yet. I wanted a solution that only used the beginner stuff I already knew. I realised that I only had to check if the first symbol is "-" and the rest is numeric. So:

if answer.isnumeric() or answer[0]=="-" and answer[1:].isnumeric():

is the solution!

EDIT: There was a typo in my solution, I meant to check if everything after the initial "-" is numeric. Otherwise an answer like "-3e" gets through. Thanks to u/Rizzityrekt28 for the catch

Find elsewhere
Top answer
1 of 5
2

Make your reading a bit shorter:

verts = []
for line in f:
    if line.startswith('v '):
        verts.append([float(val) for val in line.split()[1:]])

This should replace your full for line in f: loop. Make sure there is no other line later starting with v in your file. Maybe there is an empty line after the values, so you can stop reading there.

Now verts looks like this:

[[-543.243, -494.262, 1282.0],
 [-538.79, -494.262, 1282.0],
 [-536.422, -496.19, 1287.0],
 [-531.951, -496.19, 1287.0],
 [-527.481, -496.19, 1287.0],
 [-213.909, -223.999, 581.0],
 [-212.255, -224.384, 582.0],
 [-209.15, -223.228, 579.0],
 [-207.855, -223.999, 581.0],
 [-205.482, -223.613, 580.0],
 [-203.468, -223.613, 580.0],
 [-201.106, -223.228, 579.0],
 [-199.439, -223.613, 580.0],
 [-197.765, -223.999, 581.0],
 [-195.41, -223.613, 580.0],
 [-193.062, -223.228, 579.0],
 [-190.721, -222.842, 578.0],
 [-189.04, -223.228, 579.0],
 [-187.998, -224.384, 582.0],
 [-185.976, -224.384, 582.0],
 [-183.955, -224.384, 582.0],
 [-181.621, -223.999, 581.0],
 [-179.293, -223.613, 580.0],
 [-177.279, -223.613, 580.0],
 [-175.264, -223.613, 580.0],
 [-173.549, -223.999, 581.0],
 [-171.531, -223.999, 581.0],
 [-169.513, -223.999, 581.0],
 [-167.495, -223.999, 581.0],
 [-165.761, -224.384, 582.0],
 [-163.74, -224.384, 582.0],
 [-161.718, -224.384, 582.0],
 [-159.697, -224.384, 582.0],
 [-157.946, -224.77, 583.0],
 [-155.921, -224.77, 583.0],
 [-153.896, -224.77, 583.0],
 [-151.871, -224.77, 583.0],
 [-149.847, -224.77, 583.0],
 [-147.568, -224.384, 582.0]]
2 of 5
2

There's no problem converting to float a string of a negative number

>>> float('-5.6')
-5.6
>>> float('-531')
-531.0

Here's an example to parse a single line

>>> line = 'v -543.243 -494.262 1282'
>>> line.split()
['v', '-543.243', '-494.262', '1282']
>>> v, x, y, z = line.split()
>>> x 
'-543.243'
>>> y
'-494.262'
>>> z
'1282'

Now we convert:

>>> x = float(x)
>>> x
-543.243
๐ŸŒ
Invent with Python
inventwithpython.com โ€บ pythongently โ€บ exercise32
Exercise 32 - Convert Strings To Integers
The convertStrToInt() function must be able to handle strings representing negative integers. To do this, check if stringNum[0] (the first character in the string) is the '-' dash character. If so, we can mark an isNegative variable to True (and False otherwise).
๐ŸŒ
Codecademy Forums
discuss.codecademy.com โ€บ get help โ€บ python
.isdigit() .isnumeric() .isdecimal() - Python - Codecademy Forums
December 3, 2019 - which of the following will work for a negative number to? .isnumaric() .isdigit() .isdecimal() thanks a lot!
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_string_negative_indexing.asp
Python String Negative Indexing
Python Strings Slicing Strings Modify Strings Concatenate Strings Format Strings Escape Characters String Methods String Exercises Code Challenge Python Booleans
๐ŸŒ
Medium
mipsmonsta.medium.com โ€บ how-negative-numbers-are-represented-in-python-243c2a594015
How Negative Numbers are Represented in Python? | by Mipsmonsta | Medium
September 14, 2022 - def negativeRepToNumber(self, bitList: List[str]):""" In order to allow bit-wise operations, python represents negative number e.g. -4 as 111....11100.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 73201283 โ€บ how-do-i-differentiate-between-a-string-and-a-negative-integer-in-python
How do I differentiate between a string and a negative integer in python - Stack Overflow
Yes it is, I assume that's why they changed it in Python 3. 2022-08-02T03:04:44.503Z+00:00 ... The first if condition of the while loop converts the input to int. Meaning if the input wasn't an integer, it would return an error as it is not possible to convert a string to an int. So try this: search = input('Enter a string to continue or a negative number to exit:') while True: if search[0] == "-": if search[1].isdigit(): print('its a -ve number') break elif type(search) == str: print('Its a string OK lets run the code and search') break else: print('Please enter a valid input') break
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-segregate-positive-and-negative-integers-from-mixed-string
Python | Segregate Positive and Negative Integers from mixed string | GeeksforGeeks
April 4, 2023 - This method counts the positive and negative numbers in a list by iterating through each element using for loop.Pythona = [10, -20, 30, -40, 50, -60, 0] ... Sometimes, while working with data, we can have a problem in which we receive mixed data and need to convert the integer elements in form of strings to integers.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ valueerror: could not convert negative number string to float
r/learnpython on Reddit: ValueError: could not convert negative number string to float
August 21, 2022 -

Hello, I'm trying to convert the values of a whole column in pandas that contains string numbers to float, but I have problems in converting negative numbers like the following:

ValueError: could not convert string to float: '-39686.9720.0170'

I tried:

df['OPENING_BALANCE'] = [x.replace('\U00002013', '-') for x in df["OPENING_BALANCE"]]

but it doesn't work either, any help? :(

๐ŸŒ
Nextjournal
nextjournal.com โ€บ avidrucker โ€บ detecting-valid-number-strings-in-python
Detecting Valid Number Strings in Python - Nextjournal
Python has an "isdigit" function, but, it fails on decimal numbers and negative numbers. ... The takeaway here is that the isdigit() function will only return true if every single character in a string is a numeric character from 0 to 9.