简体   繁体   中英

Does Python 3 str.join() guarantee order?

Does Python 3 (standard implementation) guarantee that the following code always produce the string 2, 3, 1 ?

', '.join(['2', '3', '1'])

If yes, where does this feature (order-keeping) documented?

If no, what function can be used to get an order-keeping join?

str.join() joins strings in the order the sequence you passed in lists the strings.

Since you are passing in a list literal and lists have a fixed order, your sample will always produce the string output in the same order, yes.

Rule of thumb: if the order would be handled any differently, the str.join() documentation would have mentioned this explicitly. Not concatenating strings in the order the iterable provides them would be... very surprising , not to mention not very useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM