This question doesn’t seem to be PyTorch-specific, so you might get a faster and better answer e.g. on StackOverflow.
In any case, something like this would work:
chars = [c for c in '0123456789abcdefghijklmnopqrstuvwxyzäöüßABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜ!\"#$%&\'()*+,-./:;<=>?@[\\]_~€§£¥']
length = random.randint(1, 10)
s = []
for _ in range(length):
c = random.choice(chars)
print(c)
s.append(c)
s = "".join(s)
print(s)
# oPHwDR&!N
Note that this code is slow and is showing the explicit steps.
You could easily speed it up e.g. by indexing the valid characters and create the string directly instead of using the loop etc.