Reputation: 21
i'm trying to print a message on the screen and then also save that in a text file, but if i put it into a variable first, then print it, it displays with things like, and () which i don't want. I'm wondering how I would save the print output to a variable, so that i can then write it to a text file.
print("P", count, ": ", "".join(random.choice(chars) for i in range(length)))
That is what i have the print as, but if i did this:
var = "P", count, ": ", "".join(random.choice(chars) for i in range(length))
it would display as this V which I dont want it to.
('P ', 314, ': ', 'ZYIAV')
Upvotes: 2
Views: 1608
Reputation: 336468
You're looking for the format string syntax.
result = "P {}: {}".format(count, "".join(random.choice(chars) for i in range(length)))
Now result
contains the string in the format you desire and can be printed as well.
Upvotes: 1