1

i need to create a string consisting of spaces, and the length of the string needs to be determined at runtime.

here is what i am doing:

suffix = "".join([' ' for x in range(0, max)])

There must be a better way, a more clever way, and an easier way. what are the ways?

edit: well this is simpler then i imagined. there is only the 1 way and it all of the above, heh :)

3 Answers 3

6

Don't use max for a variable name.

suffix = ' ' * nb_spaces
Sign up to request clarification or add additional context in comments.

2 Comments

ha, i was wondering why there wasnt a ruby .times() operator. it was hiding there all along
Because max is the name of a built-in function. And Python will cheerfully let you shadow that function, and you won't notice it until you go to use the max function and get an error.
2

Use string "multiplication."

suffix = " " * numSpaces

Comments

1

Use string multiplication.

>>> print "x" * 5
xxxxx

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.