Skip to main content
Mathematics LibreTexts

2.3: Strings

  • Page ID
    53706
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Strings in Julia

    In many Scientific Computing fields, such as Data Science, strings arise often and it is important to understand some of the basics of them. In Julia, a string is a sequence of characters surrounded by "" (double quotes). For example:

    str ="This is a string"

    and if you enter

    typeof(str)
     

    which shows that it is a String.  Section 1.2 will go into more details about data types in Julia. The individual parts of the string are called characters, which are of type Charand are by default Unicode Characters (which will we see are super helpful). A few other helpful things about strings are

    • The length of a string is found using the length command. length(str)returns 16.

    • To access the first element of the string, type first(str), the last is found by last(str)and the 3rd character for example is str[3].In julia, string indexing starts at 1

    • To turn other data types into string, use the stringfunction. For example 
       

      string(3.0)
      "3.0"

      and again note that the double quotes indicate that this is a string. 

    String Operations

    A common string operations is concatenation, or the merging of two strings. Consider

    str1 = "The tide is high "
    str2 = "and I'm having fun."

    We can concatenate in two ways, with the *operator symbol or the string function. Both

    str1 * str2
    string(str1,str2)
    "The tide is high and I'm having fun."

    returns the same string concatenation. I find the second option clearer in that *is an odd choice for string concatenation. Many languages including java and ecmascript (javascript) use +instead for string concatenation, which I think intuitively makes more sense.  

    Another cute operation for strings is the caret operation. This could be helpful, however a not-so-helpful example is

    "Hip, hip, hooray! "^3
    "Hip, hip, hooray! Hip, hip, hooray! Hip, hip, hooray! "

    concatenates the string three times. Other important functions related to strings can be found at JULIA DOCUMENTATION ON STRINGS

    String Interpolation

    Mixing strings and other variables together is often needed and we can use the methods of concatenation as above.  However, there is a better way.  Let's use the value of x that we stored above and insert it at the end of "The value of x is ".  The following will do this

    result = "The value of x is $x"
     

    There is actually another way to do string concatenation if you use this method. Using the variables stored in str1 and str2, then 

    "$str1$str2"
     

    will also concatenate the strings.

    We can also go beyond just looking up strings in that we can execute code as well.  Let's say that we have the variable

    n=6

    If we want a string with its square, we can enter:

    "The square of $n is $(n^2)."
    "The square of 6 is 36."

    where you should notice that the code n^2 is executed since that code is within the $(). 

    Exercise

    Use the volume of a sphere \(\frac{4}{3}\pi r^3\), to calculate the volume and produce a string. For example, store r=3 and then return the result

    "The volume of a sphere of radius 3 is 113.0973355292"

    where 3 and the volume is generated with string interpolation.

     
     

    2.3: Strings is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?