Python - How to add space on each 3 characters?

前端 未结 4 1859
后悔当初
后悔当初 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:45

    a one-line solution will be

    " ".join(splitAt(x,3))
    

    however, Python is missing a splitAt() function, so define yourself one

    def splitAt(w,n):
        for i in range(0,len(w),n):
            yield w[i:i+n]
    

提交回复
热议问题