Python:在随机点的其他字符之间插入字符

mik*_*eyy 0 python

例如:

str = 'Hello world. Hello world.'
Run Code Online (Sandbox Code Playgroud)

变成:

list = ['!','-','=','~','|']
str = 'He!l-lo wor~ld|.- H~el=lo -w!or~ld.'
Run Code Online (Sandbox Code Playgroud)

DrT*_*rsa 6

import random

lst = ['!','-','=','~','|']
string = 'Hello world. Hello world.'


print ''.join('%s%s' % (x, random.choice(lst) if random.random() > 0.5 else '') for x in string)
Run Code Online (Sandbox Code Playgroud)