beginner - py unicode Q

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Apr 7 22:52:08 EDT 2007


enquiring mind wrote:

> I read the posting by Rehceb Rotkiv and response but don't know if it
> relates to my problem in any way.
>
> I only want to write German to the screen/console for little German
> programs/exercises in python.  No file w/r will be used.
>
> #! /usr/bin/env python
> # -*- coding: utf-8 -*-
> # Filename:  7P07png.py
> # SUSE Linux 10 Python 2.4.1 gedit 2.12.0
>
> print 'Ich zähle zwölf weiß Hüte.'
> print 'Wollen Sie'
> verbs = ( 'kömmen' , 'essen' , 'trinken' )
> print verbs[:3]
>
> print ' program ends '
>
> console display is: Ich zähle zwölf weiß Hüte.
> Wollen Sie
> ('k\xc3\xb6mmen', 'essen', 'trinken')
> program ends
>
> The first 2 print statements in German print perfectly to screen/console
> but not the 3rd.

In the 3rd you don't print a string, you print a tuple. Python
internally uses repr() on each item, joins them using "," and puts
parenthesis around the whole thing.
That's not usually what you want; that representation is good for
debugging. (See below)

> I also tried unicode string u'kömmen', but it did not fix problem.

Working in Unicode may be useful, but it's not your current problem.

> I found this reference section but I am not sure it applies or how to
> use it to solve my problem.:

Forget about setdefaultencoding!

> I just thought of this.  I suppose because this is py source code, it
> should not be German but a reference/key to u'strings' to print German
> text to the screen?

No...

> The ultimate console output I seek, of course, using a while or for loop
> and/or random access for second verb, for example:
> Wollen Sie kömmen?
> Wollen Sie essen?
> Wollen Sie trinken?

Had you tried this before, you would have no problems.

for verb in verbs:
  print 'Wollen Sie', verb, '?'

This way you print an individual item, and you can control exactly how
you like it displayed. Another way:

for verb in verbs:
  print 'Wollen Sie %s?' % verb

--
Gabriel Genellina




More information about the Python-list mailing list