How do I get all possible character pairs of a string using python. Each character should also pair with itself. For example taking the string 'abc' the output should be:
'aa' 'bb' 'cc' 'ab' 'ac' 'ba' 'bc' 'ca' 'cb'
How do I get all possible character pairs of a string using python. Each character should also pair with itself. For example taking the string 'abc' the output should be:
'aa' 'bb' 'cc' 'ab' 'ac' 'ba' 'bc' 'ca' 'cb'
Use itertools.product
here
Like this :
import itertools
s = "abc"
print(" ".join(map(lambda x: "".join(x), itertools.product(s, repeat=2))))
Which would result in :
aa ab ac ba bb bc ca cb cc