5

I'd like to do it in an elegant fashion:

>>> ''.zfill(5, '-')
'-----'

There's any way to initialize a string with a fill char and a counter? Of course, count may vary.

2 Answers 2

13

Just try:

>>> '-'*5
'-----'

It's that simple in Python :)

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

1 Comment

That is really great! It would never pass on my mind it could be done this way.
1

The introduction to strings in the "official" tutorial says:

Strings can be concatenated (glued together) with the + operator, and repeated with *

1 Comment

A little odd for someone coming from C ;)

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.