Printing Unicode strings in a list

Cameron Simpson cs at cskk.id.au
Thu Apr 28 07:58:21 EDT 2022


On 28Apr2022 12:32, Stephen Tucker <stephen_tucker at sil.org> wrote:
>Consider the following log from a run of IDLE:
>==================
>
>Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)]
>on win32
>Type "copyright", "credits" or "license()" for more information.
>>>> print (u"\u2551")
>>>>> print ([u"\u2551"])
>[u'\u2551']
>>>>
>==================
>
>Yes, I am still using Python 2.x - I have good reasons for doing so and
>will be moving to Python 3.x in due course.

Love to hear those reasons. Not suggesting that they are invalid.

>I have the following questions arising from the log:
>1. Why does the second print statement not produce [ ║]  or ["║"] ?

Because print() prints the str() or each of its arguments, and str() of 
a list if the same as its repr(), which is a list of the repr()s of 
every item in the list. Repr of a Unicode string looks like what you 
have in Python 2.

>2. Should the second print statement produce [ ║]  or ["║"] ?

Well, to me its behaviour is correct. Do you _want_ to get your Unicode 
glyph? in quotes? That is up to you. But consider: what would be sane 
output if the list contained the string "], [3," ?

>3. Given that I want to print a list of Unicode strings so that their
>characters are displayed (instead of their Unicode codepoint definitions),
>is there a more Pythonic way of doing it than concatenating them into a
>single string and printing that?

You could print them with empty separators:

    print(s1, s2, ......, sep='')

To do that in Python 2 you need to:

    from __future__ import print_function

at the top of your Python file. Then you've have a Python 3 string print 
function. In Python 2, pint is normally a statement and you don't need 
the brackets:

    print u"\u2551"

but print() is genuinely better as a function anyway.

>4. Does Python 3.x exhibit the same behaviour as Python 2.x in this respect?

Broadly yes, except that all strings are Unicode strings and we don't 
bothing with the leading "u" prefix.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list