【问题标题】:How Can I Split string every nth character, And Display like This?如何每隔 n 个字符拆分字符串,并像这样显示?
【发布时间】:2022-02-12 17:14:04
【问题描述】:

我想打印以下字符串:

1234567890

如下:

123
456
789
0

如何在 Python 中做到这一点?

【问题讨论】:

    标签: python string split width alphabet


    【解决方案1】:

    你可以在这里使用re.findall

    inp = '1234567890'
    output = '\n'.join(re.findall(r'.{1,3}', inp))
    print(output)
    

    打印出来:

    123
    456
    789
    0
    

    【讨论】:

      【解决方案2】:

      试试这个:

      x = "1234567890"
      n = 3
      
      y = list([x[i:i+n] for i in range(0, len(x), n)])
      
      for chunk in y:
          print(chunk)
      

      【讨论】:

        猜你喜欢
        • 2020-08-13
        • 1970-01-01
        • 2012-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多