How to use a '£' character in a string

This is the place for queries that don't fit in any of the other categories.

How to use a '£' character in a string

Postby glenmoe85 » Wed Apr 17, 2013 4:28 pm

Hey Guys,

Wondering if you can help. The string below is giving me errors as it doesn't like £ in a string. is there another way of getting the £? like a symbol converter for strings?

Code: Select all
example': {
        'demo': 'I found £10 today',


Thank you!
Last edited by Yoriz on Fri Apr 19, 2013 12:14 pm, edited 1 time in total.
Reason: Changed to a more descriptive title
glenmoe85
 
Posts: 7
Joined: Mon Apr 01, 2013 3:58 pm

Re: Symbols Currency

Postby setrofim » Wed Apr 17, 2013 4:49 pm

Stings in Python 2 can only contain ASCII charcaters (which '£' is not). To get the pound sign you need to do the following:
Code: Select all
# -*- coding: utf-8 -*-
# ^^^ you need to have this at the begining of your .py file
# (and make sure your editor's encoding is actually set to utf-8)

# ...
 
demo = u'I found £10 today'


If you don't want to mess around with encodings in your editor, you will have to specify the '£' as a unicode code point rather than a literal character:
Code: Select all
demo = u'I found \u00A310 today'

demo is now a unicode object. Unicode objects behave almost exactly like strings, however if you try to print it out or write it to a file, you'll probably get and error. To output unicode object (either to screen or to disk), you first need to encode them:
Code: Select all
print demo.encode('utf-8')
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Symbols Currency

Postby glenmoe85 » Fri Apr 19, 2013 8:31 am

Thank you Guys again!

I used the below and worked fine great!!!!!
Code: Select all
# -*- coding: utf-8 -*-
# ^^^ you need to have this at the begining of your .py file
# (and make sure your editor's encoding is actually set to utf-8)

# ...
 
demo = u'I found £10 today'
glenmoe85
 
Posts: 7
Joined: Mon Apr 01, 2013 3:58 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests