'How do I remove a random letter from a string in python?

I want to remove a random letter from a string. For example: string: "Hello world" when i run the program it prints something like "Hlo wrd"



Solution 1:[1]

You can do it with random.choice():

import random
string = "Hello world" 
string.replace(random.choice(string), '')

Output:

'Hello wold'

Solution 2:[2]

You can generate a random number to pick an index within your string to be removed, and then remove the character at that position.

from random import randrange

text = "hello world"
i = randrange(len(text)) # Get a random index within the text.
strippedText = text[:i] + text[i+1:] # Remove the character at index from our text.
print(strippedText) # Outputs 'helo world'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nin17
Solution 2 Skully