Search code examples
pythonchars

Special chars in Python


i have to use special chars in my python-application. For example: ƃ I have information like this:

U+0183 LATIN SMALL LETTER B WITH TOPBAR

General Character Properties

In Unicode since: 1.1
Unicode category: Letter, Lowercase

Various Useful Representations

UTF-8: 0xC6 0x83
UTF-16: 0x0183

C octal escaped UTF-8: \306\203
XML decimal entity: &# 387;

But when i just pust symbols into python-script i get an error:

Non-ASCII character '\xc8' ... How can i use it in strings for my application?


Solution

  • You should tell the interpreter which encoding you're using, because apparently on your system it defaults to ascii. See PEP 263. In your case, place the following at the top of your file:

    # -*- coding: utf-8 -*-
    

    Note that you don't have to write exactly that; PEP 263 allows more freedom, to accommodate several popular editors which use slightly different conventions for the same purpose. Additionally, this string may also be placed on the second line, e.g. when the first is used for the shebang.