Python - How to add space on each 3 characters?

前端 未结 4 1890
后悔当初
后悔当初 2021-01-22 12:05

I need to add a space on each 3 characters of a python string but don\'t have many clues on how to do it.

The string:

345674655

The ou

4条回答
  •  自闭症患者
    2021-01-22 12:19

    Join with '-' the concatenated of the first, second and third characters of each 3 characters:

    ' '.join(a+b+c for a,b,c in zip(x[::3], x[1::3], x[2::3]))
    

    Be sure string length is dividable by 3

提交回复
热议问题