Home > Software engineering >  Python - loop through the characters of a string until length of string reaches length of another st
Python - loop through the characters of a string until length of string reaches length of another st

Time:12-15

I'm trying to write a function that takes two strings (message and keyword) and where the latter is shorter than the former, loop over the characters in it so that the length of both strings are the same.

eg. message = "hello", keyword = "dog" – my intended output is "dogdo" so it loops over the characters inside the keyword as many times as the length of the message

here is my attempted code which repeats the entire string rather than each individual character. ie. with message = "hell" and keyword = "do", the output will be "dodododo" instead of "dodo". when len(keyword) is not a divisor of len(message), I have tried to have my output composed of the this plus the remainder. so for message="hello" and keyword="dog", intended output is "dogdo", but the output I get is "dogggdogggdogggdogggdoggg".

I know the way I'm looping this is wrong and I would really appreciate it if somebody could let me know why this is the case and how to get each character looped rather than the whole string.

if len(keyword) < len(message):
        if len(message) % len(keyword) ==0:
            for x in range(0, len(message)):
                for char in keyword:
                    keys  = char
        else:
            for x in range(0, len(message)):
                for char in keyword:
                    keys  = char
                remainder = len(message)%len(keyword)
                for x in range(0, remainder):
                    keys = char

CodePudding user response:

You can do some itertools magic or just write your own trivial generator as follows:

def func(message, keyword):
    def gc(s):
        while True:
            for e in s:
                yield e

    g = gc(keyword)
    for _ in range(len(message)-len(keyword)):
        keyword  = next(g)
    return keyword

print(func('hello', 'dog'))

Output:

dogdo

CodePudding user response:

This doesn't require any loops at all:

(keyword * ((len(message keyword)-1)//len(keyword)))[0:len(message)]

That is, make a string of enough copies of keyword to be at least as big as message, then only take a long enough prefix of that.

CodePudding user response:

You can use zip and itertools.cycle to get the effect you are looking for.

zip will iterate until the shortest iterable is exhausted and cycle will keep iterating around forever:

from itertools import cycle

message="hello"
keyword="dog"

output = ''.join([x[1] for x in zip(message, cycle(keyword))])
print(output)

Output as requested

CodePudding user response:

If I've understood your request correctly, I think something like the below is what you are looking for.

The concept here is that we have a function that does the work for us that takes as input the keyword and how long you want the string to be.

It then loops until the string has become long enough, adding 1 character at a time.

def lengthen(keyword, length):
    output = keyword
    loop_no = 0
    while len(output) < length:
        char_to_add = keyword[loop_no % len(keyword)]
        output  = char_to_add
        loop_no  = 1
    return output

longer_keyword_string = lengthen(keyword, len(message))
  • Related