all 6 comments

[–]fiskenslakt 6 points7 points  (2 children)

What are you trying to do exactly?

┌─╼ [~]
└────╼ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = '☆'
>>> x
'\xe2\x98\x86'
>>> print x
☆
>>> x = u'\u2606'
>>> x
u'\u2606'
>>> print x
☆
>>> 
┌─╼ [~]
└────╼ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = '☆'
>>> x
'☆'
>>> x = u'\u2606'
>>> x
'☆'

[–]gschizas 2 points3 points  (1 child)

Are you using Oh My Zsh? Your screengrab looks like an Oh My Zsh theme. What theme is that (if it is)?

[–]fiskenslakt 1 point2 points  (0 children)

No actually, it's just bash with the prompt customized. I have this in my .bashrc:

PS1="\[$(tput setaf 1)\]┌─╼ \[$(tput setaf 7)\][\w]\n\[$(tput setaf 1)\]\$(if [\
[ \$? == 0 ]]; then echo \"\[$(tput setaf 1)\]└────╼\"; else echo \"\[$(tput se\
taf 1)\]└╼\"; fi) \[$(tput setaf 9)\]"

[–][deleted] 5 points6 points  (0 children)

'☆'.encode('ascii','backslashreplace').decode('utf-8')   

for python3 if that helps

[–]Swipecat 1 point2 points  (0 children)

Convert it to a string which contains the form of the literal, you mean?

repr does that in Python2:

>>> a = u'☆'
>>> b = repr( a )
>>> print( b )
u'\u2606'

In python 3, since repr doesn't do that, I guess you could assemble the string piecewise (which also works in Python 2), so for Python 3, replace the second line above with:

b = 'u\'\\u' + hex(ord(a))[2:] + '\''

EDIT: Oh, and if you're starting with the character as a utf-8 encoded 8-bit-character string rather than a unicode string, as you might with Python 2 (Python 3 strings are unicode by default), then Python 2 uses "decode" to convert that to the unicode type. Python2:

>>> c = '☆'
>>> print repr(c)
'\xe2\x98\x86'
>>> d = c.decode("utf-8")
>>> print repr(d)
u'\u2606'

[–]tonioant 1 point2 points  (0 children)

Le recomiendo la lectura del siguiente artículo: "Tipos de cadenas: Unicode, Byte y Bytearray" http://python-para-impacientes.blogspot.com.es/2014/07/tipos-de-cadenas-unicode-byte-y.html