8

Вопрос по string, python – Как мне вставить пробел после определенного количества символов в строке, используя python?

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

thisisarandomsentence

Error: User Rate Limit Exceeded

this isar ando msen tenc e

Error: User Rate Limit Exceeded

def encrypt(string, length):

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded<a href="http://stackoverflow.com/questions/10055631/how-do-i-insert-spaces-into-a-string-using-the-range-function" title="how do i insert spaces into a string using the range function">stackoverflow.com/questions/10055631/&#x2026;</a>

Apr 09, 2012, 7:55 AMот

Error: User Rate Limit Exceeded

Apr 09, 2012, 8:04 AMот

Error: User Rate Limit Exceeded<a href="http://stackoverflow.com/questions/10055631/how-do-i-insert-spaces-into-a-string-using-the-range-function/10055656#10055656" title="how do i insert spaces into a string using the range function">stackoverflow.com/questions/10055631/&#x2026;</a>

Apr 09, 2012, 7:54 AMот

Error: User Rate Limit Exceeded<a href="http://stackoverflow.com/questions/10061008/generating-all-n-tuples-from-a-string/10061368" title="generating all n tuples from a string">stackoverflow.com/questions/10061008/&#x2026;</a>

Apr 09, 2012, 7:57 AMот

2ответа

1

Error: User Rate Limit Exceededitertools grouper recipe:

>>> from itertools import izip_longest
>>> def grouper(n, iterable, fillvalue=None):
        "Collect data into fixed-length chunks or blocks"
        # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
        args = [iter(iterable)] * n
        return izip_longest(fillvalue=fillvalue, *args)

>>> text = 'thisisarandomsentence'
>>> block = 4
>>> ' '.join(''.join(g) for g in grouper(block, text, ''))
'this isar ando msen tenc e'
14

def encrypt(string, length):
    return ' '.join(string[i:i+length] for i in range(0,len(string),length))

encrypt('thisisarandomsentence',4)Error: User Rate Limit Exceeded

'this isar ando msen tenc e'

RelatedQuestions