0

I just started learning python a few days ago and I came across the str.isdecimal() function. I get an error when I type the following into a python environment:

    text="123"
    Print text.isdecimal()

The error is: 'str' object has no attribute 'isdecimal'.

However, when I type the following:

    text=u"123"
    Print text.isdecimal()

Then things work out fine and I get "True" in return as expected.

My question

What is happening here? Specifically, what is the u doing to the string definition and why wont the isdecimal function work otherwise?

Thank you and sorry for if the question is too elementary.

user210552
  • 125
  • 6

2 Answers2

4

isdecimal is a method on Unicode strings, but not byte strings. u'xxx' defines a Unicode string.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0

It defines the string as the unicode type, rather than a str type.

    type(u'foo')
    <type 'unicode'>
contracode
  • 405
  • 2
  • 5