I was going through a very simple python3 guide to using string operations and then I ran into this weird error:
In [4]: # create string
string = \'L
isnumeric()
only works on Unicode strings. To define a string as Unicode you could change your string definitions like so:
In [4]:
s = u'This is my string'
isnum = s.isnumeric()
This will now store False.
Note: I also changed your variable name in case you imported the module string.
One Liners:
unicode('200', 'utf-8').isnumeric() # True
unicode('unicorn121', 'utf-8').isnumeric() # False
Or
unicode('200').isnumeric() # True
unicode('unicorn121').isnumeric() # False
if using python 3 wrap string around str as shown below
str('hello').isnumeric()
This way it behaving as expected
No, str
objects do not have an isnumeric
method. isnumeric
is only available for unicode objects. In other words:
>>> d = unicode('some string', 'utf-8')
>>> d.isnumeric()
False
>>> d = unicode('42', 'utf-8')
>>> d.isnumeric()
True