[Solved] Python-Add or remove letters at a specific location


You should do the following:

  • generate a string containing all the letters
  • use ranodm.sample() instead of random.choice() to generate a list of 3 random letters, which you then should join()
  • return an in-place list with the new elements

It’d look like this:

import string
import random


def add_str(lst):
    _letters = string.ascii_letters
    return [''.join(random.sample(set(_letters), 3)) + letter + ''.join(random.sample(set(_letters), 3))
            for letter in lst]


print(add_str(['a', 'o', 'r', 'x', ' ', 's', 'n', ' ', 'k', 'p', 'l', 'q', 't']))

> ['FUsaeNZ', 'pASoiTI', 'XfbrUXe', 'ZyKxhSs', 'lIJ blk', 'bJXseAI', 'uFcnUeQ', 'KRd wfF', 'VyPkjvq', 'CbwpCro', 'QOTlNfi', 'UNuqRDe', 'hEjtnIv']

I supposed you want different letters at the beginning and the end of each letter from the string. If you need them to be the same, you can handle it. Since you didn’t provide any example, I answered your exact question and what I understood from it. If you need something else (and it looks like it’s the case from the comments), you have where to start from anyway

4

solved Python-Add or remove letters at a specific location