I need a function generateAllStrings(n, alphabet)
to do something like this:
generateAllStrings(4, ['a','b'])
>>> ["aaaa", "aaab", "aaba", "aabb", "abaa", .... , "bbba", "bbbb"]
In other words, generateAllStrings(n, alphabet)
should return all the possible strings of length n over the chars in the list alphabet.
Is there such a function in itertools or something?
The len() function returns the number of items in an object. When the object is a string, the len() function returns the number of characters in the string.
>>> [''.join(i) for i in itertools.product("ab",repeat=4)]
['aaaa', 'aaab', 'aaba', 'aabb', 'abaa', 'abab', 'abba', 'abbb', 'baaa', 'baab', 'baba', 'babb', 'bbaa', 'bbab', 'bbba', 'bbbb']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With