user13112262
user13112262

Reputation:

Printing randomized letters between every letter in a word

I am making a program in Python3 that does a number of things to a file, one of the things being adding random numbers and letters between each word in the string. I want the random characters to not be on the ends of the words, for example, for the word Python:

P-y-t-h-o-n

Where the - are where the random characters would be. So far I have:

def addCharacters(string, strength, randomLettersNumbers):
    newString = ''
    for c in string:
        letters = ''
        for k in range(strength):
            letters += random.choice(randomLettersNumbers)
        newString += c + letters
    return newString

Where strength is the amount of random characters to print between each letter, string is what i'm printing the letters between, and randomLettersNumbers is my list of random characters. However, it is printing them on the outside ends of the words too, like -P-y-t-h-o-n-, how would I fix it so it is only on the inside?

Upvotes: 0

Views: 184

Answers (4)

Georgina Skibinski
Georgina Skibinski

Reputation: 13387

Try:

import numpy as np

def addCharacters(stringIn, strength, randomLettersNumbers):
    addedLetters=np.random.choice(randomLettersNumbers, size=(len(stringIn)-1, strength))
    return ''.join(map(''.join, zip(stringIn, map(''.join, addedLetters))))+stringIn[-1]

Outputs:

>>> randomL=list("qwertyuiopasdfghjklzxcvbnm1234567890")
>>> print(addCharacters("Python", 2, randomL))

P7oyumtcghbmoern

Upvotes: 0

francoMG
francoMG

Reputation: 36

It is only necessary to add random letters and numbers after each letter of the original string, except the last one that only needs to be added. The code could look like this:

def addCharacters(string, strength, randomLettersNumbers):
    newString = ''
    n = len(string)
    for i in range(n-1):
        letters = ''
        for k in range(strength):
            letters += random.choice(randomLettersNumbers)
        newString += string[i] + letters
    newString += string[n-1]
    return newString

addCharacters("python", 2, ["X", "2"]) # 'pX2y22t22hX2oX2n'

Well your question is something different, what you need is to separate the word by spaces before applying "addCharacters"

string = "This is a sentence"
array = string.split(" ")
newArray = [addCharacters(word, 1, ["a", "b", "c"]) for word in array]
newString = " ".join(newArray)
print(newString)

Upvotes: 1

christian_schmidt
christian_schmidt

Reputation: 30

I tried Your code and it seems that the character inserted in front of the string might be caused by a blank space. To avoid inserting the random character at the end just slice the string and add the last character in the return statement:

def addCharacters(string, strength, randomLettersNumbers):
    newString = ''
    for c in string[:-1]:
        letters = ''
        for k in range(strength):
            letters += random.choice(randomLettersNumbers)
        newString += c + letters
    return newString+string[-1]+' '

def processSentence(string,strength,randomLettersNumbers):
    return "".join([addCharacters(s,strength,randomLettersNumbers) for s in string.split()])

This code should produce the results You're looking for by splitting Your sentence. Than it applies the method individually for each word and adds a space at the end. The join() method rejoins the modified strings.

Upvotes: 0

mz496
mz496

Reputation: 870

Focus on this line: for c in string:

Your code is inserting extra contents after every letter in the string, but it looks like you need to insert extra contents after every letter except the last one. You'll need a way to loop over all characters except the last one; there are several ways to do that (for example, using indices, or list slicing, since strings have a lot of list-like behavior in Python).

On that note, I don't think you would actually see random characters before the first letter of string. For debugging purposes, hardcoding the random characters instead of calling random.choice would help to visualize what's happening.

Upvotes: 0

Related Questions