如何以10个字符的行显示字符串

时间:2019-11-03 18:45:08

标签: python

我有以下字符串

ACTGACTGACTGACTGACTGACTGACTGAC

我想以10个字符的行显示此字符串,每行分成5个字符的组

所以我的第一行是:

ACTGA CTGAC 

如何实现?

5 个答案:

答案 0 :(得分:3)

您可以使用textwrap模块将数据分成10个字符的块,然后格式化每一行:

import textwrap

s = 'ACTGACTGACTGACTGACTGACTGACTGAC'

out = '\n'.join(line[:5] + ' ' + line[5:] for line in textwrap.wrap(s, 10))

print(out)

输出:

ACTGA CTGAC
TGACT GACTG
ACTGA CTGAC

答案 1 :(得分:0)

简单的方法是使用理解力,请参见示例:

>>> the_string="ACTGACTGACTGACTGACTGACTGACTGAC"
>>> size_s=5 
>>> var_buff = ''
>>> output=[the_string[var_buff-size_s:var_buff] for var_buff in range(size_s, len(the_string)+size_s,size_s)]
>>> print(output)
['ACTGA', 'CTGAC', 'TGACT', 'GACTG', 'ACTGA', 'CTGAC']

答案 2 :(得分:0)

通过这种方法,您可以设置每行和每个块的字符数:

s = 'ACTGACTGACTGACTGACTGACTGACTGAC'
m = 5  # chunk size
n = 10 # line size
tmp = [(s[i:i+m], s[i+m:i+n]) for i in range(0, len(s), n)]

for chunk1, chunk2 in tmp:
    print(chunk1 + ' ' + chunk2)

输出:

ACTGA CTGAC
TGACT GACTG
ACTGA CTGAC

答案 3 :(得分:0)

您可以编写自己的生成器函数:

A cookie associated with a cross-site resource at https://cloudflare.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.

哪种产量(但也可以根据其他长度进行调整)

<?php
    header('Set-Cookie: cross-site-cookie=name; SameSite=None; Secure');
?>

答案 4 :(得分:0)

类似的东西应该起作用。仅适用于五个字符的组

string='ACTGACTGACTGACTGACTGACTGACTGAC'

string = list(string)

while string:
    if len(string)>=10:
        print(''.join(string[:5])+' '+''.join(string[5:10])+'\n')
        string = string[10:]
    else:
        print(''.join(string[:5]))
        string = string[5:]