Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

julia

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?

like image 949
Julia Learner Avatar asked Sep 06 '18 20:09

Julia Learner


People also ask

How do you repeat a character in a string in Python?

In Python, we utilize the asterisk operator to repeat a string. This operator is indicated by a “*” sign. This operator iterates the string n (number) of times.

How do you make a string repeating characters?

Java has a repeat function to build copies of a source string: String newString = "a". repeat(N); assertEquals(EXPECTED_STRING, newString);

Which operator is used to repeat a string n number of times?

Answer. Explanation: The * operator can be used to repeat the string for a given number of times. Writing two string literals together also concatenates them like + operator.


Video Answer


1 Answers

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)
"========================================"
like image 182
Julia Learner Avatar answered Oct 20 '22 05:10

Julia Learner