-1

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'

shady
  • 11
  • 2
  • 1
    Does this answer your question? [What is the best way to generate all possible three letter strings?](https://stackoverflow.com/questions/7074051/what-is-the-best-way-to-generate-all-possible-three-letter-strings) – Chris Jan 19 '22 at 04:37
  • What have you tried so far? Have you looked at [`itertools`](https://docs.python.org/3/library/itertools.html)? – Jan Wilamowski Jan 19 '22 at 05:12

1 Answers1

0

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
Achxy_
  • 1,171
  • 1
  • 5
  • 25
  • Code-only questions are not very useful, especially for beginner questions like this one. I suggest you break down your convoluted one-liner into understandable parts and add some textual explanation. – Jan Wilamowski Jan 19 '22 at 08:29