Julia Learner
Julia Learner

Reputation: 2912

In Julia, how do I repeat a character n times creating a string like in Python?

In Python it is easy to create a string of n characters:

>>> '=' * 40
'========================================'

However, in Julia the above does not work. What is the Julia equivalent to the Python code above?

Upvotes: 13

Views: 5442

Answers (1)

Julia Learner
Julia Learner

Reputation: 2912

In Julia you can replicate a single character into a string of n characters, or replicate a single-character string into a string of n characters using the ^ operator. Thus, either a single-quoted character, '=', or a double-quoted single character, "=", string will work.

julia> '='^40  # Note the single-quoted character '='
"========================================"
julia> "="^40  # Note the double-quoted string "="
"========================================"

Another way to do do the same thing is:

julia> repeat('=', 40)
"========================================"

julia> repeat("=", 40)
"========================================"

Upvotes: 27

Related Questions