Unicode characters cannot print correctly in the terminal python

I wrote a very simple program that tells me the unicode value of some characters.

This is the program:

< /p>

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

characters = [u'T', u'ב ', u'€', u'木', u'♥']

for character in characters:

print(character + "has the unicode value :\t "+str(hex(ord(character))) + "\n")

It gives this output:

T has the unicode value: 84

ב has the unicode value: 1489

€ has the unicode value: 8364

wood has the unicode value: 26408

♥ has the unicode value: 9829

I noticed that the output format is correct when I copy and paste, but on my computer, the second line is displayed as follows in the terminal

p>

has the unicode value: 1489 ב

I also tried to put the output into a file and use vim to view the file, it also looks like this, it should be printed first The character is printed last. This makes me think it is printed correctly but not displayed correctly. What could cause this to happen?

You can use Unicode LEFT-RIGHT OVERRIDE (LRO) character 0x202D to cover the right alignment of Hebrew characters Behavior.

characters = [u'T', u'ב', u'€', u'木', u'♥']

for character in characters:

print(chr(0x202D) + character + "has the unicode value :\t"+str(hex(ord(character))) + "\ n")

To (on OS X terminal):

T has the unicode value: 0x54

ב has the unicode value: 0x5d1

€ has the unicode value: 0x20ac

wood has the unicode value: 0x6728

♥ has the unicode value: 0x2665

Thank you @guribe94 for identifying the problem.

You may find the string format easier to read:

print ("%s%s has the unicode value :\t 0x%04x\n" %
(chr(0x202D), character, ord(character)))

I wrote a very simple program to tell me the unicode value of some characters.

This is the program:

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

characters = [u'T', u'ב', u'€', u '木', u'♥']

for character in c haracters:

print(character + "has the unicode value :\t"+str(hex(ord(character))) + "\n")

It gives Got this output:

T has the unicode value: 84

ב has the unicode value: 1489

€ has the unicode value: 8364

Wood has the unicode value: 26408

♥ has the unicode value: 9829

I noticed that when I copy and paste When the output format is correct, but on my computer, the second line is displayed as follows in the terminal

has the unicode value: 1489 ב

I also Trying to put the output into a file and use vim to view the file, it also looks like this, the characters that should be printed first are printed last. This makes me think it is printed correctly but not displayed correctly. What could cause this to happen?

The Unicode LEFT-RIGHT OVERRIDE (LRO) character 0x202D can be used to override the right alignment behavior of Hebrew characters.

characters = [u'T', u'ב', u'€', u'木', u'♥']

for character in characters:

print(chr(0x202D) + character + "has the unicode value :\t"+str(hex(ord(character))) + "\n")

Give( On OS X terminal):

T has the unicode value: 0x54

ב has the unicode value: 0x5d1
< br />€ has the unicode value: 0x20ac

Wood has the unicode value: 0x6728

♥ has the unicode value: 0x2665

Thanks @guribe94 identifies the problem.

You may find the string format is easier to read:

print("%s%s has the unicode value :\ t 0x%04x\n" %
(chr(0x202D), character, ord(character)))

Leave a Comment

Your email address will not be published.