sep=''
ignore whiteSpace.
see the code to understand.Without sep=''
from itertools import permutations
s,k = input().split()
for i in list(permutations(sorted(s), int(k))):
print(*i)
output:
HACK 2
A C
A H
A K
C A
C H
C K
H A
H C
H K
K A
K C
K H
using sep=''
The code and output.
from itertools import permutations
s,k = input().split()
for i in list(permutations(sorted(s), int(k))):
print(*i,sep='')
output:
HACK 2
AC
AH
AK
CA
CH
CK
HA
HC
HK
KA
KC
KH