JokerMartini
JokerMartini

Reputation: 6147

Create string with placeholder character

Is there a one liner in python that will give me the same result as below? All I need to supply is a int value and then it return a string using @.

results in a string with '@' appended for each loop.

padding = ''
for x in range(5): 
    padding += '@'
print padding

Results:

@@@@@

Upvotes: 1

Views: 884

Answers (2)

o11c
o11c

Reputation: 16136

If this is in the middle of some other formatting, you might use something like:

'{:@<{}}'.format('', 5)

(the inner {} expands to 5 before the outer formatting applies)

Upvotes: 1

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96246

Yes, strings are sequence types, so the easiest way is:

print '@'*5

Upvotes: 4

Related Questions