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?
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.
Java has a repeat function to build copies of a source string: String newString = "a". repeat(N); assertEquals(EXPECTED_STRING, newString);
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.
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)
"========================================"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With