5

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?

tobias47n9e
  • 2,233
  • 3
  • 28
  • 54

2 Answers2

0

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)

Community
  • 1
  • 1
Darcinon
  • 159
  • 6
  • Both of these solutions didn't work for me. Although reversing the order works for latin letters, they don't seem to do the same for Arabic. Confusing :) – tobias47n9e Nov 14 '15 at 21:41
0

Some terminals do support printing Arabic.

For example, GNOME Terminal (in Ubuntu for example) works like this:

Screenshot

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.

Flimm
  • 136,138
  • 45
  • 251
  • 267
  • Hmm are you sure this looks right? My terminal displays something like `على.` – n. m. could be an AI Feb 23 '22 at 15:43
  • @n.1.8e9-where's-my-sharem. Could you send a screenshot? The text in your comment looks similar to the screenshot to me. – Flimm Feb 23 '22 at 17:56
  • Here https://imgur.com/a/btxP2Uz – n. m. could be an AI Feb 23 '22 at 21:08
  • @n.1.8e9-where's-my-sharem. The word على is displayed correctly in your screenshot. The line has not been set to right-to-left mode, though. If it did, the dot would appear on the left, not on the right. An Arabic speaker is writing from right to left, the word is written first, and then the dot, and so the dot is expected to be on the left, not the right. Or maybe you are talking about the spacing in my screenshot? – Flimm Feb 24 '22 at 07:58
  • "The line has not been set to right-to-left mode" That's correct. The Unicode specs say the paragraph should be set to LTR or RTL according to the first directional character of the paragraph, but not all text processors do that. – n. m. could be an AI Feb 24 '22 at 08:22