Skip to main content
 首页 » 编程设计

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

2025年05月04日18zfyouxi

例如:

str = 'Hello world. Hello world.' 

变成:

list = ['!','-','=','~','|'] 
str = 'He!l-lo wor~ld|.- H~el=lo -w!or~ld.' 

请您参考如下方法:

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)