dimanche 17 juillet 2016

String slicing not working for accented characters

String slicing in Python 2.7 is very useful to get a substring. This works nicely for ASCII characters, for example

>>> s = "Antonio"
>>> s[5:7]
'io'

but fails in the presence of accented characters, for example

>>> s = "António"
>>> s[5:7]
'ni'

What is the safe way to get the correct substring irrespective of the characters present in the original string?

UPDATE My configuration info is the following:

Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 12:54:16) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Thanks

Aucun commentaire:

Enregistrer un commentaire