【发布时间】:2022-02-12 17:14:04
【问题描述】:
我想打印以下字符串:
1234567890
如下:
123
456
789
0
如何在 Python 中做到这一点?
【问题讨论】:
标签: python string split width alphabet
我想打印以下字符串:
1234567890
如下:
123
456
789
0
如何在 Python 中做到这一点?
【问题讨论】:
标签: python string split width alphabet
你可以在这里使用re.findall:
inp = '1234567890'
output = '\n'.join(re.findall(r'.{1,3}', inp))
print(output)
打印出来:
123
456
789
0
【讨论】:
试试这个:
x = "1234567890"
n = 3
y = list([x[i:i+n] for i in range(0, len(x), n)])
for chunk in y:
print(chunk)
【讨论】: