2

In Python 3, this:

alternates={'Qabr Ḩamdān', 'قبور حمدان', 'Qabr Hamdan', 'Qubur Hamdan', 'Qubūr Ḩamdān', 'qbwr hmdan'}
for x in alternates:
    print(12.1, x, 13.2)

produces this:

12.1 Qabr Ḩamdān 13.2
12.1 قبور حمدان 13.2
12.1 Qabr Hamdan 13.2
12.1 Qubur Hamdan 13.2
12.1 Qubūr Ḩamdān 13.2
12.1 qbwr hmdan 13.2

Notice the Arabic string in the second line is out of order. Why?

martineau
  • 119,623
  • 25
  • 170
  • 301
Lars Ericson
  • 1,952
  • 4
  • 32
  • 45
  • 1
    Perhaps because Arabic is right-to-left? – k_ssb Sep 17 '18 at 00:58
  • what version of python3 are you using? I just tested on Python 3.6.5 and it produces the expected output – bunbun Sep 17 '18 at 00:59
  • 1
    Possible duplicate of [Python Right-to-Left and Left-to-Right printed nicely](https://stackoverflow.com/questions/42556063/python-right-to-left-and-left-to-right-printed-nicely) – k_ssb Sep 17 '18 at 01:01
  • @bunbun I've replicated the issue on 3.6.1: https://repl.it/repls/HonorableScentedDatamart – k_ssb Sep 17 '18 at 01:02
  • @pkpnd: The answer to the linked question might be how to fix or avoid the issue, but doesn't really explain **Why**, which is all the OP wants to know. – martineau Sep 17 '18 at 01:33
  • @martineau The answer to "Why" is given by my first comment. – k_ssb Sep 17 '18 at 01:34
  • IMO the answer to "Why" is because of the mixing of glyphs from RTL and LTR alphabets together. – martineau Sep 17 '18 at 01:38

1 Answers1

2

That's normal. Arabic does that in Unicode.

Arabic is written right-to-left, and when an Arabic speaker types text, they're going to expect the Arabic to come out right-to-left... but they're going to expect numbers like 13.2 to come out as left-to-right chunks embedded within the overall right-to-left layout. The Unicode Bidirectional Algorithm does its best to meet this expectation. The details are really complicated and laid out in Unicode Standard Annex #9.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Answers the question asked, so I'll upvote even though there's no mention of how to work around the problem (which one would assume the OP also would like to know even it they didn't explicitly ask for it). – martineau Sep 17 '18 at 01:47
  • If I understand correctly, the Bidirectional Algorithm is performed at the level of displaying the glyphs, ie. outside Python's control and is entirely up to the terminal/editor rendering the output. But if so, why would the Python version make a difference, as suggested in the comments to the OP? Is this a timing issue (string concatenation/flushing in different order inside `print`)? – lenz Sep 17 '18 at 07:50
  • ... or maybe bunbun and pkpnd see different results simply because their respective terminals implement the bidi algorithm differently? – lenz Sep 17 '18 at 07:51
  • @lenz: Probably a terminal thing, especially if one used a terminal and the other used a browser-based thing. – user2357112 Sep 17 '18 at 08:23