-3

I am wondering how to randomly change n characters in a string, e.g.

orig = 'hello'

mod = 'halle'

that I want to randomly select two positions (orig[1] and orig[4]) in the string, and replace the chars in the positions of the original string (hello) with randomly selected chars (a and e here), results in a new string halle.

Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
daiyue
  • 7,196
  • 25
  • 82
  • 149
  • 1
    Have you tried anything? – Morgan Thrapp Nov 04 '16 at 14:31
  • 1
    Have you checked the [random](https://docs.python.org/3/library/random.html) and [string](https://docs.python.org/3/library/string.html) modules? Those two have all the things you want. – Lafexlos Nov 04 '16 at 14:32
  • Possible duplicate of [Random strings in Python](http://stackoverflow.com/questions/2030053/random-strings-in-python) – Jamie Counsell Nov 04 '16 at 14:33
  • 1
    What part are you having trouble with, determining a random location in a string, generating a random character or replacing a character in a string? We can't tell from what you wrote exactly where you need help. Please edit your question and provide the code that you have written so far and tell us exactly where you need help. – Bill W Nov 04 '16 at 14:34
  • 1
    Also, the problem statement could use some more clarification - e.g. is it acceptable to update the same character position more than once, as long as a total of N changes are made, or must it be N distinct locations? – twalberg Nov 04 '16 at 14:47

2 Answers2

2
import random
import string
orig='hello'

char1=random.choice(string.ascii_lowercase)  #random character1
char2=random.choice(string.ascii_lowercase)  #random character2

while char1 == char2:                   # #check if both char are equal
    char2=random.choice(string.ascii_lowercase)

ran_pos1 = random.randint(0,len(orig)-1)  #random index1
ran_pos2 = random.randint(0,len(orig)-1)  #random index2

while ran_pos1 == ran_pos2:            #check if both pos are equal
    ran_pos2 = random.randint(0,len(orig)-1)

orig_list = list(orig)
orig_list[ran_pos1]=char1
orig_list[ran_pos2]=char2
mod = ''.join(orig_list)
print(mod)
R__raki__
  • 847
  • 4
  • 15
  • 30
0

If you just want to change the different characters at random index in a string the below function will help. This script will ask for the input string(i.e., word) and the total places/ indexes ((i.e.,)value or 'n' places) you need to change with random characters, and this will print the modified string as needed.

import random
import string

# Method to change N characters from a string with random characters.
def randomlyChangeNChar(word, value):
    length = len(word)
    word = list(word)
    # This will select the two distinct index for us to replace
    k = random.sample(range(0,length),value)
    for index in k:
        # This will replace the characters at the specified index with 
        # the generated characters
        word[index] = random.choice(string.ascii_lowercase)
    # Finally print the string in the modified format.
    print("" . join(word))

# Get the string to be modified
string_to_modify = raw_input("Enter the string to be replaced...\n")

# get the number of places that needed to be randomly replaced
total_places = input("Enter the total places that needs to be modified...\n")

# Function to replace 'n' characters at random
randomlyChangeNChar(string_to_modify, total_places)

Output

 Enter the string to be replaced...
 Hello
 Enter the total places that needs to be modified...
 3
 Hcado
  • Add some explanation with answer for how this answer help OP in fixing current issue – ρяσѕρєя K Jul 19 '17 at 15:32
  • I provided some explanation in the code itself, that is the generic solution Since he can use any words and any number of characters can be replaced by random characters in that word, Hope this helps :-) @ρяσѕρєяK – Chandra Prakash V Apr 04 '19 at 08:19