Unicode and Standard Strings

Mixing with standard strings

  • Unicode strings and standard strings can be mixed together
  • Operators (+, %, etc.)
  • Dictionary keys
  • String methods
  • Built-in functions and modules

Examples

     a = "Hello"
     b = u"World"
     c = a + b

General approach

  • When mixed in an operator, standard strings are always coerced to Unicode.
     c = unicode(a) + b
  • When standard strings expected, unicode is encoded into 8-bit string
     f = open(b)   # f = open(b.encode())
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 58
July 26, 2001, beazley@cs.uchicago.edu
>>>