如何利用python 按规则生成字符串

输入一串字符串,在每个字母间随机加入符号. 生成一批不重复的字符串。并保存在excel 文件中,运行结束后,自动打开excel文件。

大概逻辑这样

先 计算 字符串长度 ,  可以插入空格是 长度 -1from itertools import combinations 
算出所有的组合
遍历插入字符串 ,输出


贴下代码,可供参考。


import random,copy

#获取所有的符号   
from string import punctuation

test = "helloworld"
arry =[i for i in test]
    
#取出符号组合(假设需要10组不同的组合)
symbol_list = []
while True:
    res = random.sample(punctuation,len(test)-1)
    if res not in symbol_list:
        symbol_list.append(res)
    if len(symbol_list) == 10:
        break
        
#将符号插入到字符串中,生成10组新的字符串
for symbols in symbol_list:
    index = 1
    arry2 = copy.copy(arry)
    for symbol in symbols:
        arry2.insert(index,symbol)
        index += 2
    new_str = ''.join(arry2)
    print(new_str)
    
    

img