0

I wrote a python script which doesn't declare any utf-8 or anything. When I execute the script, I see character 'u' added. Why is that? I am using Python2.7 version.

access_token = token.json()['access_token']
print(' "' + access_token + '"' )
url = 'https://api.yelp.com/v3/businesses/search'
bearer = 'Bearer ' + access_token
print(bearer)
header = {'Authorization':'Bearer ' + access_token}
print(header)
params = {'location': 'San Bruno',
          'term': 'Restaurant',
          'radius':'25 miles',
          'locale':'en_US',
          'sort_by': 'rating',       
          'limit':'50'
         }

I see a strange character u being added

enter image description here

sofs1
  • 3,834
  • 11
  • 51
  • 89
  • 2
    That's not a character. That indicates the String's encoding iirc. – Carcigenicate Aug 03 '17 at 23:30
  • @Carcigenicate Thanks. Honestly When I typed "Why is a new character 'u' getting added when I execute the Python script?" it doesn't show the duplicate question that you have mentioned now (which means the search was not based on context, rather words used). Since now this question has been marked as duplicate, soon I will be blocked for asking questions. This happened to me before. – sofs1 Aug 04 '17 at 00:00
  • @Carcigenicate May I know what is meant by iirc – sofs1 Aug 04 '17 at 00:02
  • I searched "Python string u prefix" to find it. And "iirc" means "if I recall correctly". – Carcigenicate Aug 04 '17 at 00:07
  • 1
    And as far as I know, having a question closed as a duplicate doesn't contribute to being question banned. Even if it did, having 1 question closed won't cause it. You must have had several closed very close to each other. – Carcigenicate Aug 04 '17 at 00:09

2 Answers2

5

All JSON strings are unicode: "A string is a sequence of zero or more Unicode characters". In Python 2.x, the repr of a of a unicode string includes "u" at the beginning, since this is how a unicode string literal looks like.

While print() prints the str() of a dictionary, the str of a dictionary calls the repr of the included items.

moshez
  • 36,202
  • 1
  • 22
  • 14
2

It's just an indicator of a Unicode string in Python 2.x. The notation gives a visual indicator of the string type. When printing containers, strings are quoted and Unicode strings have the extra u:

>>> s = 'abc'    # byte string
>>> s            # just quotes, no u
'abc'
>>> s.decode('ascii')  # Make it a Unicode string
u'abc'
>>> u = s.decode('ascii')
>>> u
u'abc'
>>> print(u)   # both print the same way
abc
>>> print(s)
abc

Containers use the notation by default. You have to print the elements to see the content without quotes or u:

>>> L = ['abc',u'def']
>>> L
['abc', u'def']
>>> print(L)
['abc', u'def']
>>> for i in L: print i
...
abc
def
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251