user6882757
user6882757

Reputation:

How to split the string in python and repeat it and join

My Code is below. I want to count the letters in the output

s = string and n is number of times to repeat. n = 10, means the string s = "aba" is repeated over the course of 10 letters. i.e abaabaabaa.

s, n = input().strip(), int(input().strip())
print(s.count("a")  

Out = 7

my code is below

a = 'aba'
t = list(a)
n = 3
new_list = []
if n  <=  len(t):
    for i in range(n):
        new_list.append(t[i])
        m = t + new_list
    print (''.join(m))
elif n > len(t):
    x,y = divmod(n,len(t))
    if y == 0:
        new_list.append(a * x)
    else:
        new_list.append((a * x) + ''.join(map(str,t[:y])))

if n is large then need to loop like len(list(s)) = 3, if n = 10, divide 10/3 and we got 3 equal part and we got 1 remainder

Upvotes: 0

Views: 144

Answers (4)

dms
dms

Reputation: 1388

You could use something like this:

def repeat_n(s, n):
  return (s * (n // len(s) + 1))[:n]

s = 'aba'
print(repeat_n(s, 2))
# ab

print(repeat_n(s, 10))
# abaabaabaa

print(repeat_n(s, 12))
# abaabaabaaba

n // len(s) gives you the number of times we can repeat s and stay below n characters so n // len(s) + 1 is the number of time we can repeat s to get at least n characters (plus potentially some others).

With that number we just repeat the string with s * (n // len(s) + 1) and then take the first n characters (s * (n // len(s) + 1))[:n].

Hope this helps.

EDIT: fixed for python 3

Upvotes: 0

Scovetta
Scovetta

Reputation: 3152

Here's another option, using the * operator for strings:

s = 'aba'
n = 10

out = (s * (n // len(s)))[:n]
print(out)

Breaking it down:

n // len(s) => This is just the number of whole times s will need to be repeated
+ 1        => We add one to this, so our string will be a little longer than needed
s *        => This repeats s that number of times
[:n]       => Takes the first n characters of the string

So while it isn't as readable, it's very fast. Compared to the accepted answer:

def original_calc():
  out = ''
  for i in range(n):
    out += s[i % len(s)]
  result = out

def new_calc():
    result = (s * (n // len(s)))[:n]

import timeit
s = 'aba'
n = 10
>>> timeit.timeit(original_calc, number=100000)
0.20508930005598813
>>> timeit.timeit(new_calc, number=100000)
0.027835099957883358

Upvotes: 0

GZ0
GZ0

Reputation: 4268

In case the ultimate intention is to iterate over the generated string rather than directly printing it as output, an alternative is to use the itertools library.

from itertools import cycle, islice

s = "aba"
n = 10
for c in islice(cycle(s), n):
    ... do other stuff ...

Another scenario is that you try to compare the generated string with another string s2 of length n. In that case, islice can be omitted.

for c, c2 in zip(cycle(s), s2):  # zip() truncates cycle(s) to the same length as s2
     ... operate on c and c2 ...

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195643

What are you looking for is modulo (%) operator:

s = 'aba'
n = 10

out = ''
for i in range(n):
    out += s[i % len(s)]

print(out)

Prints:

abaabaabaa

Upvotes: 0

Related Questions