Kieron Holmes
Kieron Holmes

Reputation: 60

Manipulating string to repeat based on the length of another string

I am working on a python project, where I am required to include an input, and another value (which will be manipulated).

For example, If I enter the string 'StackOverflow', and a value to be manipulated of 'test', the program will make the manipulatable variable equal to the number of characters, by repeating and trimming the string. This means that 'StackOverflow' and 'test' would output 'testtesttestt'.

This is the code I have so far:

originalinput = input("Please enter an input: ")
manipulateinput = input("Please enter an input to be manipulated: ")
while len(manipulateinput) < len(originalinput):

And I was thinking of including a for loop to continue the rest, but am not sure how I would use this to effectively manipulate the string. Any help would be appreciated, Thanks.

Upvotes: 1

Views: 233

Answers (4)

Reut Sharabani
Reut Sharabani

Reputation: 31339

Try something like this:

def trim_to_fit(to_trim, to_fit):
     # calculate how many times the string needs
     # to be self - concatenated
     times_to_concatenate = len(to_fit) // len(to_trim) + 1
     # slice the string to fit the target
     return (to_trim * times_to_concatenate)[:len(to_fit)]

It uses slicing, and the fact that a multiplication of a X and a string in python concatenates the string X times.

Output:

>>> trim_to_fit('test', 'stackoverflow')
'testtesttestt'

You can also create an endless circular generator over the string:

# improved by Rick Teachey
def circular_gen(txt):
    while True:
        for c in txt:
            yield c

And to use it:

>>> gen = circular_gen('test')
>>> gen_it = [next(gen) for _ in range(len('stackoverflow'))]
>>> ''.join(gen_it)
'testtesttestt'

Upvotes: 2

Rick
Rick

Reputation: 45251

What you need is a way to get each character out of your manipulateinput string over and over again, and so that you don't run out of characters.

You can do this by multiplying the string so it is repeated as many times as you need:

mystring = 'string'
assert 2 * mystring == 'stringstring'

But how many times to repeat it? Well, you get the length of a string using len:

assert len(mystring) == 6

So to make sure your string is at least as long as the other string, you can do this:

import math.ceil # the ceiling function
timestorepeat  = ceil(len(originalinput)/len(manipulateinput))
newmanipulateinput = timestorepeat * manipulateinput

Another way to do it would be using int division, or //:

timestorepeat  = len(originalinput)//len(manipulateinput) + 1
newmanipulateinput = timestorepeat * manipulateinput

Now you can use a for loop without running out of characters:

result = '' # start your result with an empty string 
for character in newmanipulateinput: 
    # test to see if you've reached the target length yet
    if len(result) == len(originalinput):
        break
    # update your result with the next character
    result += character 
    # note you can concatenate strings in python with a + operator 
print(result)

Upvotes: 0

Jon Clements
Jon Clements

Reputation: 142156

An itertools.cycle approach:

from itertools import cycle

s1 = 'Test'
s2 = 'StackOverflow'
result = ''.join(a for a, b in zip(cycle(s1), s2))

Given you mention plaintext - a is your key and b will be the character in the plaintext - so you can use this to also handily manipuate the pairing...

I'm taking a guess you're going to end up with something like:

result = ''.join(chr(ord(a) ^ ord(b)) for a, b in zip(cycle(s1), s2))
# '\x07\x11\x12\x17?*\x05\x11&\x03\x1f\x1b#'
original = ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(cycle(s1), result))
# StackOverflow

Upvotes: 3

xnx
xnx

Reputation: 25518

There are some good, Pythonic solutions here... but if your goal is to understand while loops rather than the itertools module, they won't help. In that case, perhaps you just need to consider how to grow a string with the + operator and trim it with a slice:

originalinput = input("Please enter an input: ")
manipulateinput = input("Please enter an input to be manipulated: ")
output = ''
while len(output) < len(originalinput):
    output += manipulateinput
output = output[:len(originalinput)]

(Note that this sort of string manipulation is generally frowned upon in real Python code, and you should probably use one of the others (for example, Reut Sharabani's answer).

Upvotes: 2

Related Questions