Display string multiple times

Python

Python Problem Overview


I want to print a character or string like '-' n number of times.

Can I do it without using a loop?.. Is there a function like

print('-',3)

..which would mean printing the - 3 times, like this:

---

Python Solutions


Solution 1 - Python

Python 2.x:

print '-' * 3

Python 3.x:

print('-' * 3)

Solution 2 - Python

The accepted answer is short and sweet, but here is an alternate syntax allowing to provide a separator in Python 3.x.

print(*3*('-',), sep='_')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHickView Question on Stackoverflow
Solution 1 - Pythonuser447688View Answer on Stackoverflow
Solution 2 - PythonOlivier MelançonView Answer on Stackoverflow