2

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')
1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.