0

For example, I have the string s1 = "lets go to the mall" and a second string s2 = "hello"

In Python, how can I manipulate the s2 string to equal the length of s1.

s2 would then look like:

s2 = "hellohellohellohell" which would have the same number of characters as s1.

zondo
  • 19,901
  • 8
  • 44
  • 83
jason00
  • 1
  • 1
  • 3

9 Answers9

4

Here's one approach:

s1 = 'lets go to the mall'
s2 = 'hello'
s2 = ''.join(s2[i % len(s2)] for i in range(len(s1)))
print(s2)  # "hellohellohellohell"

Edit: Here's an explanation for those not as familiar with Python or programming =]

  • ''.join(...) takes an iterable, which is something you can iterate through, and joins all of those elements together with the empty string in between. So, if what's on the inside is an iterable of letters, it'll join all of those letters together.
  • range(len(s1)) produces an iterable of all numbers 0 to len(s1) - 1. The number of numbers in this iterable is equal to the length of s1.
  • s2[i] means the letter in the string s2 at index i. So, if s2 = 'hello', then s2[0] = 'h', s2[1] = 'e', etc.
  • i % len(s2) means i modulo len(s2), or the remainder when you divide i by the length of s2.
  • So, putting that all together, this code first creates an iterable of letters looping through s2 as many times as it needs to in order to get len(s1) many letters, then joins them all together with the empty string in between.
Karin
  • 8,404
  • 25
  • 34
  • 1
    Cleanest solution by far. – juanpa.arrivillaga Aug 18 '16 at 01:35
  • Instead of building the string up one letter at a time, build it up one copy of `s2` at a time, then trim the excess off the end. `''.join(s2 for i in range(0,len(s1),len(s2)))[:len(s1)]` (which is similar to some of the other answers). – chepner Aug 18 '16 at 01:40
  • @chepner I think all of the solutions here have pros and cons. For this solution, I wanted to go for readability/cleanliness over optimizing for string concatenation and performance. I do think other solutions are likely faster, but I found this easier to comprehend than the slicing approaches :) – Karin Aug 18 '16 at 01:49
  • 1
    Agreed; my own answer is *extremely* inefficient, but very short :) – chepner Aug 18 '16 at 01:56
  • I smiled when I saw that! I think your solution is probably the best in terms of speed, but maybe not so much on memory ;) I think mine isn't as speedy, but seems pretty memory-efficient in that it doesn't create additional string to chop off later. But of course, all of this is pretty trivial in most practical situations. – Karin Aug 18 '16 at 01:59
2

Itertools is the answer. More specifically takewhile and cycle

import itertools

s1 = "lets go to the mall"
s2 = "Hello"

print ("".join(s for _, s in itertools.takewhile(lambda t: t[0] < len(s1), enumerate(itertools.cycle(s2)))))

Or even simpler (using islice):

print ("".join(itertools.islice(itertools.cycle(s2)), len(s1)))
smac89
  • 39,374
  • 15
  • 132
  • 179
1

// is integer division which finds the whole multiples. % is the modulo (remainder)

Multiply s2 my the number of times it can go into s1 and then use slicing to add the remaining portion of s2.

s3 = s2 * (len(s1) // len(s2)) + s2[:(len(s1) % len(s2))]

>>> s3
'hellohellohellohell'
Alexander
  • 105,104
  • 32
  • 201
  • 196
0
(s2 * (len(s1)//len(s2) + 1))[:len(s1)]
acw1668
  • 40,144
  • 5
  • 22
  • 34
0

Basically multiply s2 by the math.floor of the two lengths divided, then add the remainder of the string:

def extend(s1, s2):
    return s2*int(math.floor(len(s1)/len(s2)))+s2[:len(s1) % len(s2)]

>>> extend("lets go to the mall", "hello")
'hellohellohellohell'
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

Off of the top of my head, and you'll have to forgive me, you could use a function like this:

def string_until_whenever(s1, s2):
i = len(s1)
x = 0
newstring = ""
while i != 0:
    newstring = newstring + s2[x]
    x += 1
    i -= 1
    if x == len(s2) - 1:
         x = 0
return newstring
Riley J
  • 3
  • 3
0

Inefficient, but simple. (The multiplication makes a string much longer than it needs to be.)

n = len(s1)
s3 = (s2*n)[:n]
chepner
  • 497,756
  • 71
  • 530
  • 681
0

I think there are many possible solutions. amongst many possible solutions my answer is :

s2 = s2*(len(s1)/len(s2)+1)
s2 = s2[0:len(s1)]
Jemin Lee
  • 41
  • 1
0

May not be the cleanest solution but you can also do this, using string multiplication and string slicing:

def string_until_whenever(s1, s2):
    temp = ""    
    if len(s2) > len(s1):
        temp = s2
        s2 = s1
        s1 = temp

    new_string = ""
    multiply_by = len(s1)/len(s2)
    modulo = len(s1) % len(s2)    
    new_string = s2 * multiply_by
    new_string = new_string + s2[0:modulo]    

    return new_string


print(string_until_whenever("lets go to the mall", "hello"))
#Outputs: hellohellohellohell
Mestica
  • 1,489
  • 4
  • 23
  • 33