Skip to content Skip to sidebar Skip to footer

How To Generate A Range Of Strings From Aa... To Zz

I am trying to create a Python program where it accepts a string and displays permutations based on that. For example. If the user enters '[AAA] Grim' it should generate words sta

Solution 1:

input = '[AAA] Grim'
n = len(input.split(' ')[0]) - 2
s = input[n+2:]
a = [''.join(x)+s for x in itertools.product(string.ascii_lowercase, repeat=n)]

Solution 2:

If I understood what you meant the following will do:

import re
import string
import itertools

user_in = input("Enter string: ")
p_len = user_in.index(']') - user_in.index('[')
text = user_in[user_in.index(']')+1:]
print('\n'.join([ ''.join(p) + text for p in itertools.combinations_with_replacement(string.ascii_lowercase,r=p_len-1)]))

Will produce (an extract):

Enter string: [AA] Grim
aa Grim
ab Grim
ac Grim
ad Grim
ae Grim

Solution 3:

>>> import string
>>> ltrs = string.lowercase
>>> [''.join([a,b]) for a in ltrs for b in ltrs]

The code is from here: https://mail.python.org/pipermail/tutor/2005-July/040117.html

I found it and it works great!


Post a Comment for "How To Generate A Range Of Strings From Aa... To Zz"