# String multiplication possible in LaTeX as in Python?

I'd like to set a string multiplication in LaTeX in order to print n times a specific string or character.

Just like in Python one would do :

print('mystring' * 3)


to get mystringmystringmystring.

How do I do this in LaTeX?

## migrated from stackoverflow.comNov 16 '17 at 13:45

This question came from our site for professional and enthusiast programmers.

## 3 Answers

If you can include the pgffor package, then it is as easy as doing \foreach \i in {1,...,3}{mystring}. For easier use, it can be wrapped in a command:

\newcommand{\repeatn}[2]{\foreach \i in {1, ..., #1} {#2}}

Usage: \repeatn{3}{mystring}

• Waww fairly simple ! Thanks @gjulianm! it'll simplify a lot my document ! – Etsaf Nov 16 '17 at 21:04

Here's the mandatory L3 approach with \prg_replicate:

\documentclass{article}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\prg_replicate:nn {11}{na}~Batman!
\ExplSyntaxOff
\end{document}

• Great ! Thanks @Paulo Cereda I'll see for putting it into a newcommand :-) – Etsaf Nov 16 '17 at 21:06

Without any additional packages, you can use the \loop command:

\documentclass{article}

\newcounter{tmp}
\newcommand{\strmult}[2]{\setcounter{tmp}{0}\loop\stepcounter{tmp}{#1}\ifnum\value{tmp}<#2\repeat}

\begin{document}

\strmult{foo}{4}\strmult{bar}{5}

\end{document}