Create a string from any values using the print function.


The string function in Julia is used to create a string from any values using the print function.

julia> string(10, "apples", true)

Here are some common examples of its use:

  1. Concatenate multiple values into a string:

    julia> x = 5;
    julia> y = "bananas";
    julia> z = false;
    julia> string(x, " ", y, " are ", z)
    "5 bananas are false"

    This example combines the values of x, y, and z into a single string with appropriate spacing and formatting.

  2. Convert non-string values to strings:

    julia> a = 3.14;
    julia> b = 42;
    julia> string("The value of pi is ", a, " and the answer to everything is ", b)
    "The value of pi is 3.14 and the answer to everything is 42"

    In this example, the numeric values a and b are automatically converted to strings and concatenated with the other strings.

  3. Create an empty string:
    julia> empty_str = string()

    If no arguments are provided, string() returns an empty string.

Common mistake example:

julia> nums = [1, 2, 3];
julia> string(nums)
ERROR: MethodError: no method matching string(::Array{Int64,1})

In this example, an array nums is directly passed to string, resulting in an error. To convert an array to a string, you need to unpack its elements or use a different approach.

See Also

ascii, base64decode, Base64DecodePipe, base64encode, Base64EncodePipe, bin, bits, bytestring, charwidth, chomp, chop, chr2ind, contains, endswith, escape_string, graphemes, ind2chr, iscntrl, istext, isupper, isvalid, join, lcfirst, lowercase, lpad, lstrip, normalize_string, num2hex, parseip, randstring, readuntil, replace, repr, rpad, rsplit, rstrip, search, searchindex, split, startswith, string, stringmime, strip, strwidth, summary, takebuf_string, ucfirst, unescape_string, uppercase, utf16, utf32, utf8, wstring,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: