Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:50:25+00:00 2026-05-28T07:50:25+00:00

For instance, I have the following string: Hello how are you today, [name]? How

  • 0

For instance, I have the following string:

Hello how are you today, [name]?

How would I go about randomly placing characters between a random choice of words but not [name]? I already I have this following piece of code but I was hoping there is a better way of going about it.

string = 'Hello how are you today, [name]?'
characters = 'qwertyuioplkjhgfdsazxcvbnm,. '
arr = string.rsplit(" ")

for i in range(0, len(arr)):
    x = arr[i]
    if x == '[name]':
        continue
    if (random.randint(0,2)==1) :
        rnd=random.randint(1,len(x)-2)
        tmp1 = random.randint(0,len(characters))
        rndCharacter = characters[tmp1:tmp1+1]
        x = x[0:rnd] + rndCharacter + x[rnd+1:]
        arr[i] = x

" ".join(arr)

> Hellio how are yoy todsy, [name]?"

Though this replaces the character with a another random character. What way would I go about having it randomly replace or place a random character after or before a character as well?

Basically I’m just trying to simulate a sort of typo generator.

Thanks

Update on my code so far:

string = 'Hey how are you doing, [name]?'
characters = 'aeiou'
arr = string.rsplit(" ")
for i in range(0, len(arr)):
    x = arr[i]
    if x == '[name]': continue
    if len(x) > 3:
        if random.random() > 0.7:
            rnd = random.randint(0,len(x)-1)
            rndCharacter = random.choice(characters)
            if random.random() > 0.7:
                x = x[0:rnd] + rndCharacter + x[rnd+1:]
            else:
                x = x[:rnd] + rndCharacter + x[rnd:]
            arr[i] = x
    else:
        if random.random() > 0.7:
            rnd = random.randint(0,len(x)-1)
            rndCharacter = random.choice(characters)
            x = x[:rnd] + rndCharacter + x[rnd:]
            arr[i] = x
print " ".join(arr)

> Hey houw are you doiang, [name]?

UPDATE:

Maybe my final update for the code, hopefully this will help someone out some point in the future

def misspeller(word):
    typos = { 'a': 'aqwedcxzs',
              'b': 'bgfv nh',
              'c': 'cdx vf',
              'd': 'desxcfr',
              'e': 'e3wsdfr4',
              'f': 'fredcvgt',
              'g': 'gtrfvbhyt',
              'h': 'hytgbnju',
              'i': 'i8ujko9',
              'j': 'juyhnmki',
              'k': 'kiujm,lo',
              'l': 'loik,.;p',
              'm': 'mkjn ,',
              'n': 'nhb mjh',
              'o': 'o9ikl;p0',
              'p': 'p0ol;[-',
              'q': 'q1asw2',
              'r': 'r4edft5',
              's': 'swazxde',
              't': 't5rfgy6',
              'u': 'u7yhji8',
              'v': 'vfc bg',
              'w': 'w2qasde3',
              'x': 'xszcd',
              'y': 'y6tghu7',
              'z': 'zaZxs',
              ' ': ' bvcnm',
              '"': '"{:?}',
              '\'': '[;/\']',
              ':': ':PL>?"{',
              '<': '<LKM >',
              '>': '>:L<?:',
              ';': ';pl,.;[',
              '[': '[-p;\']=',
              ']': '=[\'',
              '{': '{[_P:"}+',
              '}': '}=[\']=',
              '|': '|\]\'',
              '.': '.l,/;',
              ',': ',lkm.'
            }

    index = random.randint(1,len(word)-1)
    letter = list(word[:index])[-1].lower()
    try:
        if random.random() <= 0.5:
            return word[:index] + random.choice(list(typos[letter])) + word[index:]
        else:
            return word[:index-1] + random.choice(list(typos[letter])) + word[index:]
    except KeyError:
        return word

def generate(self, s, n, safe_name):
    misspelled_s = ''
    misspelled_list = []
    for item in s.split(' '):
        if n:
            if safe_name in item:
                misspelled_list.append(item)
            else:
                r = random.randint(0,1)
                if r == 1 and len(re.sub('[^A-Za-z0-9]+', '', item)) > 3:
                    misspelled_list.append(misspeller(item))
                    n -= 1
                else:
                    misspelled_list.append(item)
        else:
            misspelled_list.append(item)
    return ' '.join(misspelled_list)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:50:26+00:00Added an answer on May 28, 2026 at 7:50 am
    import random
    
    def misspeller(word):
        characters = 'qwertyuioplkjhgfdsazxcvbnm,. '
        rand_word_position = random.randint(-1,len(word))
        rand_characters_position = random.randint(0,len(characters)-1)
    
        if rand_word_position == -1:
            misspelled_word = characters[rand_characters_position] + word 
        elif rand_word_position == len(word):
            misspelled_word = word + characters[rand_characters_position] 
        else:
            misspelled_word = list(word)
            misspelled_word[rand_word_position] = characters[rand_characters_position]
            misspelled_word = ''.join(misspelled_word)        
        return misspelled_word
    
    s = 'Hello how are you today, [name]?'
    misspelled_s = ''
    misspelled_list = []
    for item in s.split(' '):
        if '[name]' in item:
            misspelled_list.append(item)
        else:
            misspelled_list.append(misspeller(item))
    misspelled_s = ' '.join(misspelled_list)
    print misspelled_s
    

    Examples of what I’m getting from misspelled_s are:

    'Hellk howg ars youf poday, [name]?'
    'Heylo how arer y,u todab, [name]?'
    'Hrllo hfw  are zyou totay, [name]?'
    

    Edited to clean up a couple of mistakes and omissions on first copy.

    Edit 2 If you don’t want every word to be affected you can modify the for loop in the following way:

    for item in s.split(' '):
        n = random.randint(0,1)
        if '[name]' in item:
            misspelled_list.append(item)
        elif n == 1:
            misspelled_list.append(misspeller(item))
        else:
            misspelled_list.append(item)
    

    You can modify the probability that a word is modified by changing how n is generated e.g. n = random.randint(0,10)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
We have the following.. public class Foo { public string Name { get; private
I have the following string that I would like to Huffman-encode and store efficiently
Given I have code like the following: void foo() { String str = hello;
I have written the following Utility class to get an instance of any class
For instance I have a string like this : abc123[*]xyz[#]098[~]f9e [*] , [#] and
I have the following code: InputStream reportFile = MyPage.this.getClass().getResourceAsStream(test.jrxml); HashMap<String, String> parameters = new
I get the following string expression provided: ChildObject.FullName ...where ChildObject is an instance property
I have following snippet: static void Main(string[] args) { var container = new UnityContainer();
I have following classes. In instance of BE (let's say objBE) i want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.