'Print Arabic or any Right-to-Left writing-system string to Linux terminal using Python
The very simple example is:
city = "المكلا"
print(city)
I am expecting the output to be:
المكلا
But in fact the ouput is the reverse string (the letters look a little different because they have a start-, middle- and end-form). I can't paste it here, because copy-pasting corrects the order of the string again.
How can I print Arabic correctly to the Linux terminal? The surounding text is left-to-right (LTR) and only this line needs to be right-to-left (RTL). Is there a UFT-8 character that can tell ther terminal that?
Solution 1:[1]
To create a string with the RTL character:
rtl = u'\u200f'
Python 3 uses UTF strings by default, so in that case the "u" in front of the string would be unnecessary.
If the problem is actually that the terminal just can't render correctly, you could manually reverse the string.
test = 'Hello world'
test = test[::-1]
# test == 'dlroW olleH'
There is also the python-bidi library which might be able to help. (source)
Solution 2:[2]
Some terminals do support printing Arabic.
For example, GNOME Terminal (in Ubuntu for example) works like this:
In this example, I typed echo ', followed by the letter ? than the letter ? than the letter ?, followed by .' and then I pressed Enter
As you can see, the word was displayed correctly on the screen, with the letters displayed from right to left, and joined, although the monospace font is showing a gap between the first letter and the second. This terminal did not however enable right-to-left mode for the whole line (in which case, the dot at the end would have been displayed all the way to the left). It also did not set the text alignment to the right for the line. The terminal mlterm does have these features, so you may want to try mlterm.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Flimm |

