Home > Enterprise >  How to add a letter after every letter in a string?
How to add a letter after every letter in a string?

Time:03-12

I want to create an encryption script that encrypts the string given to it, here's how it went.

# First, I created a list of the string I got
string = '16271'
string_list = []
for letter in string:
    string_list.append(letter)

Then, I have a list called encrypt_list which the letters which I want to add in order to encrypt my string.
So, I use the following code to add a random letter from the encrypt_list after each letter/component in the string_list and then join the list and print as a string.

for i in range(0, len(string) - 1):
    string_list.insert(for letter in string_list: string_list.index(letter)   1, encrypt_list[random.randint(0, len(encrypt_list) - 1)])

print("The encrypted string is: ")
print(''.join(string_list))

I expected the output to be: 1A6b2n781 (I bolded the letter to show my actual string in it) But I am getting an error, that I cannot use the for loop in the insert function, and I cannot find another way of doing that, please help. Hope I make my problem clear

CodePudding user response:

Something like this?

# First, I created a list of the string I got
import random
string = '16271'
string_list = []
for letter in string:
    string_list.append(letter)


the_other_list = ['lorem', 'dorem', 'forem', 'borem']

for i in range(0, len(the_other_list)):
    the_other_list[i] = string_list[random.randint(0, len(string_list) - 1)]   the_other_list[i]

print(''.join(the_other_list))

Result example: 1lorem2dorem2forem7borem

CodePudding user response:

You can use a for loop, adding one letter to the list at a time, then adding a randomly selected letter immediately afterwards (if we're not processing the last letter in the list). I've used constants from string to define the space of characters to sample from; you can adjust this as you see fit.

This should be simpler than trying to do repeated insertion in the middle of the list (where you'd have to handle memory shifting as you're inserting, plus it'd get slower for larger texts because you'd be attempting to insert in the middle of a list).

# First, I created a list of the string I got
import random
import string
encrypt_text = string.ascii_uppercase   string.ascii_lowercase   string.digits
plaintext = '16271'
letters = []
for index, letter in enumerate(plaintext):
    letters.append(letter)
    if index != len(plaintext) - 1:
        letters.append(random.choice(encrypt_text))

print("The encrypted string is: ")
print(''.join(letters))

CodePudding user response:

Not sure what your encrypted_list looks like, but if it's a list of letters, this would work:

import random

string = '16271'
encrypted_list = ['r', 't', 's', 'o', 'j', 'e']
encrypted_string = ''.join([s   random.choice(encrypted_list) for s in string])

CodePudding user response:

Based on how you defined the problem, I would recommend implementing it as a generator:

import random
import string

def _gen_string(s):
    alphabet = string.ascii_letters
    for c in s:
        yield c
        yield random.choice(alphabet)

Then you can use that as the basis for your encryption:

def encrypt(s):
    return ''.join(_gen_string(s))

encrypt('16271')
# 1J6P2U7Z1i

Of course, this is not really encryption. It's obscurity and obscurity is not a form of security that should be relied upon :-)

  • Related