Skip to main content
Engineering LibreTexts

5.2: String Operations

  • Page ID
    39363
  • \( \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}}\)

    Text data, too, has many things that can be done to it. For now, let’s just learn a few techniques for concatenating strings (tacking one onto the end of another) trimming strings (removing whitespace1 from the ends) and changing their case (upper/lower). See Figure 5.2.1 for a list.

    clipboard_e399899ea7511f0d307be52d7c9cd0928.png

    Figure \(\PageIndex{1}\): A few of Python’s string methods.

    The plus sign is an operator, like the mathematical ones in Figure 5.1.1: it’s used to concatenate (append) one string to another. Example:

    Code \(\PageIndex{1}\) (Python):

    x = "Lady"

    y = "Gaga"

    z = x + y

    print(z)

    The second one is slapped right on the end of the first; there’s no spaces or punctuation. If you wanted to insert a space, you’d have to do that explicitly with a string-that-consists-of-only-a-space (written as the three characters: quote, space, quote), like this:

    Code \(\PageIndex{2}\) (Python):

    first = 'Dwayne'

    last = "Johnson"

    full = first + ' ' + last

    print(full)

    Punctuation marks, too, have to be included literally, and it can be tricky to get everything typed in the right way:

    Code \(\PageIndex{3}\) (Python):

    first = 'Dwayne'

    last = "Johnson"

    nick = 'The Rock'

    full = first + ' "' + nick + '" ' + last

    print("Don't ya just love {}?".format(full))

    | Don't ya just love Dwayne "The Rock" Johnson?

    Punctuation marks, too, have to be included literally, and it can be tricky to get everything typed in the right way:

    Code \(\PageIndex{4}\) (Python):

    first = 'Dwayne'

    last = "Johnson"

    nick = 'The Rock'

    full = first + ' "' + nick + '" ' + last

    print("Don't ya just love {}?".format(full))

    | Don't ya just love Dwayne "The Rock" Johnson?

    Stare at that line beginning with “full =” and see if you can figure out why each punctuation mark is where it is, and why there are spaces between some of them and not between others.

    By the way, here’s a bit of a head-scratcher at first:

    Code \(\PageIndex{5}\) (Python):

    matriculation_year = "2021"

    graduation_year = matriculation_year + 4

    print("Imma graduate in {}!".format(graduation_year))

    | Imma graduate in 20214!

    Whoa – wut? That’s a lot of tuition. The problem here is that matriculation_year was defined as a string, not an integer (note the quotes). So the + sign meant concatenation, not addition. Remember: a string-consisting-only-of-digits is not the same as a number. (If you remove the quotes from the first line, your mom will breathe easier and you’ll get the result you expect.)

    The other items in Figure 5.2.1 are methods: they have an initial dot (“.”) and they must be called “on a string” (meaning, a string variable name must immediately precede them). They also take no arguments, which means that a lonely, empty pair of bananas comes after their name when they are called. Examples:

    Code \(\PageIndex{6}\) (Python):

    shop_title = " carl's ICE cream "

    print(shop_title)

    print(shop_title.strip())

    print(shop_title.upper())

    print(shop_title.lower())

    print(shop_title.title())

    | carl's ICE cream

    | carl's ICE cream

    | CARL'S ICE CREAM

    | carl's ice cream

    | Carl'S Ice Cream

    (You can’t see the trailing spaces in the output, but you can see the leading ones.)

    You can even combine method calls back to back like this:

    Code \(\PageIndex{7}\) (Python):

    print(shop_title.strip().upper())

    These operations are for more than mere prettiness. They’re also used for data cleansing, which is often needed when dealing with messy, real-world data sets. If, say, you asked a bunch of people on a Web-based survey which Fredericksburg ice cream store they prefer, lots of them will name Carl’s: but they’ll type the capitalization every which way, forget the apostrophe, clumsily add spaces to one end (or even both, or even in the middle), yet they’ll all have in mind the same luscious vanilla cones. One step towards conflating all these different expressions to the same root answer would be trimming the whitespace off the ends and converting everything to all lower-case. More surgical operations like removing punctuation or spaces in the middle is a bit trickier; stay tuned.


    This page titled 5.2: String Operations is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Stephen Davies (allthemath.org) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.