How can I convert Unicode to uppercase to print it?

后端 未结 5 919
遥遥无期
遥遥无期 2020-12-10 01:04

I have this:

>>> print \'example\'
example
>>> print \'exámple\'
exámple
>>> print \'exámple\'.upper()
EXáMPLE

W

5条回答
  •  一个人的身影
    2020-12-10 01:11

    I think there's a bit of background we're missing here:

    >>> type('hello')
    
    
    >>> type(u'hello')
    
    

    As long as you're using "unicode" strings instead of "native" strings, the operators like upper() will operate with unicode in mind. FWIW, Python 3 uses unicode by default, making the distinction largely irrelevant.

    Taking a string from unicode to str and then back to unicode is suboptimal in many ways, and many libraries will produce unicode output if you want it; so try to use only unicode objects for strings internally whenever you can.

提交回复
热议问题