Pound Sign in Python Program
Archive - Originally posted on "The Horse's Mouth" - 2009-09-15 17:59:42 - Graham EllisHow do I get a pound sign up in Python? A regular question ans the regular answer is to use a unicode string:
>>> saying = u'It will cost \u00a310.00'
>>> print saying
It will cost £10.00
>>>Which is all well and good, but people want to be able to type the pound sign into the source code too. But that gives problems.
Source code file:
92:1 grahamellis$ cat ff
val = "it cost £9.00"
print val
92:1 grahamellis$And when I try to run that, I get:
92:1 grahamellis$ python ff
File "ff", line 1
SyntaxError: Non-ASCII character '\xc2' in file ff on line 1, but no
encoding declared; see http://www.python.org/peps/pep-0263.html
for details
92:1 grahamellis$Well - it tells me to declare the coding, so I will. That's a comment in a special format in one of the first two lines of the source file. Here's a modified source file:
# -*- coding: utf-8 -*-
uv = u'That will cost you £1340.00'
print uvAnd it runs beautifully
92:1 grahamellis$ python ahha
That will cost you £1340.00
92:1 grahamellis$See also The Pyhon Unicode HowTo ... but note that the # -*- coding: latin-1 -*- example there isn't the one for the pound sign (It also produced an umlauted A!) and you want utf-8