Function Repository Resource:

StringTuples

Source Notebook

Generate a list of all tuples of characters from a string

Contributed by: Stephen Wolfram & Jan Mangaldan

ResourceFunction["StringTuples"]["string",n]

generates a list of all possible length-n strings formed from the characters in "string".

ResourceFunction["StringTuples"]["string",{m,n}]

generates a list of all possible strings with lengths m through n.

ResourceFunction["StringTuples"][{"string1","string2",}]

generates a list of all possible strings whose ith character is from "stringi".

Details and Options

The characters of "string" are treated as distinct, so that ResourceFunction["StringTuples"]["string",n] for a string of length k gives output of length kn.

Examples

Basic Examples (3) 

Generate all length-3 strings containing A and B:

In[1]:=
ResourceFunction["StringTuples"]["AB", 3]
Out[1]=

Generate strings with length 1 through 3:

In[2]:=
ResourceFunction["StringTuples"]["AB", {1, 3}]
Out[2]=

All ways of picking a character from each string:

In[3]:=
ResourceFunction["StringTuples"][{"xy", "+-*/", "z"}]
Out[3]=

Properties and Relations (2) 

Duplicated characters are treated as distinct:

In[4]:=
ResourceFunction["StringTuples"]["AAB", 3]
Out[4]=

Results from Outer involve extra sublists:

In[5]:=
Outer[StringJoin, Characters["AB"], Characters["uvw"]]
Out[5]=
In[6]:=
ResourceFunction["StringTuples"][{"AB", "uvw"}]
Out[6]=

Version History

  • 3.0.0 – 26 August 2020
  • 2.0.0 – 26 December 2019
  • 1.0.0 – 04 December 2019

Related Resources

License Information