Printed length of a string in python

后端 未结 3 1016
旧巷少年郎
旧巷少年郎 2021-02-14 19:35

Is there any way to find (even a best guess) the \"printed\" length of a string in python? E.g. \'potaa\\bto\' is 8 characters in len but only 6 characters wide pr

3条回答
  •  时光取名叫无心
    2021-02-14 19:57

    The bash shell had exactly the same need, in order to know when the user's typed input wraps to the next line, in the presence of non-printable characters in the prompt string. Their solution was to not even try - instead, they require that anyone setting a prompt string put \[ and \] around non-printing portions of the prompt. The printed length is calculated to be the length of the string, with these special sequences and all text between them filtered out. (The special sequences are omitted on output, of course.)

提交回复
热议问题