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

5
  • 2
    That's not a character. That indicates the String's encoding iirc. Commented Aug 3, 2017 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. Commented Aug 4, 2017 at 0:00
  • @Carcigenicate May I know what is meant by iirc Commented Aug 4, 2017 at 0:02
  • I searched "Python string u prefix" to find it. And "iirc" means "if I recall correctly". Commented Aug 4, 2017 at 0: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. Commented Aug 4, 2017 at 0:09

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.