-6

I need to insert a string (character by character) into another string at every 3rd position For example:- string_1:-wwwaabkccgkll String_2:- toadhp Now I need to insert string2 char by char into string1 at every third position So the output must be wwtaaobkaccdgkhllp Need in Python.. even Java is ok

So i tried this

Test_str="hiimdumbiknow"
challenge="toadh"
new_st=challenge [k]
Last=list(test_str)
K=0
For i in range(Len(test_str)):
    if(i%3==0):
        last.insert(i,new_st)
    K+=1

and the output i get thitimtdutmbtiknow

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
  • Your code gives me more questions than it answers. What is `k`? Why are `For` and `Len` in title-case? What is `K` (upper-case this time) for? – Klaus D. Nov 19 '22 at 11:27
  • Oh shoot... sorry i forgot to remove it – Naren M.S Nov 20 '22 at 19:13
  • Does this answer your question? [Insert some string into given string at given index](https://stackoverflow.com/questions/4022827/insert-some-string-into-given-string-at-given-index) – wovano Nov 23 '22 at 08:13

4 Answers4

1

You can split test_str into sub-strings to length 2, and then iterate merging them with challenge:

def concat3(test_str, challenge):
    chunks = [test_str[i:i+2] for i in range(0,len(test_str),2)]
    result = []
    i = j  = 0
    while i<len(chunks) or j<len(challenge):
        if i<len(chunks):
            result.append(chunks[i])
            i += 1
        if j<len(challenge):
            result.append(challenge[j])
            j += 1
    return ''.join(result)

test_str  = "hiimdumbiknow"
challenge = "toadh"

print(concat3(test_str, challenge))
# hitimoduambdikhnow

This method works even if the lengths of test_str and challenge are mismatching. (The remaining characters in the longest string will be appended at the end.)

C-3PO
  • 1,181
  • 9
  • 17
0

You can split Test_str in to groups of two letters and then re-join with each letter from challenge in between as follows;

import itertools
print(''.join(f'{two}{letter}' for two, letter in itertools.zip_longest([Test_str[i:i+2] for i in range(0,len(Test_str),2)], challenge, fillvalue='')))

Output:

hitimoduambdikhnow

*edited to split in to groups of two rather than three as originally posted

bn_ln
  • 1,648
  • 1
  • 6
  • 13
0

you can try this, make an iter above the second string and iterate over the first one and select which character should be part of the final string according the position

def add3(s1, s2):
    def n():
        try:
            k = iter(s2)
            for i,j in enumerate(s1):
                yield (j if (i==0 or (i+1)%3) else next(k))
        except:
            try:
                yield s1[i+1:]
            except:
                pass 
    return ''.join(n()) 
-1
def insertstring(test_str,challenge):
    result = ''
    x = [x for x in test_str]
    y = [y for y in challenge]
    j = 0
    for i in range(len(x)):
        if i % 2 != 0 or i == 0:
            result += x[i]
        else:
            if j < 5:
                result += y[j]
                result += x[i]
                j += 1
    get_last_element = x[-1]
    return result + get_last_element


print(insertstring(test_str,challenge))

#output: hitimoduambdikhnow
Joelinton
  • 61
  • 6