Skip to main content
Engineering LibreTexts

1.4: String Basics

  • Page ID
    117516
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    Learning Objectives

    By the end of this section you should be able to

    • Use the built-in len() function to get a string's length.
    • Concatenate string literals and variables using the + operator.

    Quote marks

    A string is a sequence of characters enclosed by matching single (') or double (") quotes. Ex: "Happy birthday!" and '21' are both strings.

    To include a single quote (') in a string, enclose the string with matching double quotes ("). Ex: "Won't this work?" To include a double quote ("), enclose the string with matching single quotes ('). Ex: 'They said "Try it!", so I did'.

    Table 1.3 Rules for strings.
    Valid string Invalid string
    "17" or '17'
    17
    "seventeen" or 'seventeen'
    seventeen
    "Where?" or 'Where?'
    "Where?'
    "I hope you aren't sad."
    'I hope you aren't sad.'
    'The teacher said "Correct!" '
    "The teacher said "Correct!" "
    Concepts in Practice \(\PageIndex{1}\): Valid and invalid strings

    Which of the following is a string?

    1. Hello!
    2. 29
    3. "7 days"
    Answer

    c. A string that is enclosed by matching double quotes is a valid string. Note that '7 days' is also a string.

    Concepts in Practice \(\PageIndex{2}\): Valid and invalid strings

    Which line of code assigns a string to the variable email?

    1. "fred78@gmail.com"
    2. "email = fred78@gmail.com"
    3. email = "fred78@gmail.com"
    Answer

    c. The string, fred78@gmail.com, is enclosed in matching double quotes, and is assigned to the variable email. A string can contain characters such as "@" or "." in addition to letters and numbers.

    Concepts in Practice \(\PageIndex{3}\): Valid and invalid strings

    Which is a valid string?

    1. I know you'll answer correctly!
    2. 'I know you'll answer correctly!'
    3. "I know you'll answer correctly!"
    Answer

    c. The string has matching double quotes. Since a single quote is a part of this string, double quotes must be used.

    Concepts in Practice \(\PageIndex{4}\): Valid and invalid strings

    Which is a valid string?

    1. You say "Please" to be polite
    2. "You say "Please" to be polite"
    3. 'You say "Please" to be polite'
    Answer

    c. Note that the enclosing single quotes (') are not part of the string but the double quotes (") are.

    len() function

    A common operation on a string object is to get the string length, or the number of characters in the string. The len() function, when called on a string value, returns the string length.

    Checkpoint: Using len() to get the length of a string
    Concepts in Practice \(\PageIndex{5}\): Applying len() function to string values

    What is the return value for len("Hi Ali")?

    1. 2
    2. 5
    3. 6
    Answer

    c. The len() function returns the number of characters in the string. 6 characters exist in "Hi Ali" which are "H", "i", " ", "A", "l", and "i".

    Concepts in Practice \(\PageIndex{6}\): Applying len() function to string values

    What is the length of an empty string variable ("")?

    1. undefined
    2. 0
    3. 2
    Answer

    b. The length of an empty string equals 0.

    Concepts in Practice \(\PageIndex{7}\): Applying len() function to string values

    What is the output of the following code?

    number = "12"
    number_of_digits = len(number)
    print("Number", number, "has", number_of_digits, "digits.")
    
    1. Number 12 has 12 digits.
    2. Number 12 has 2 digits.
    3. Number 12 has number_of_digits digits.
    Answer

    b. The number_of_digits variable's value is 2 , and as such, the output is "Number 12 has 2 digits." .

    Concatenation

    Concatenation is an operation that combines two or more strings sequentially with the concatenation operator (+). Ex: "A" + "part" produces the string "Apart".

    Checkpoint: Concatenating multiple strings
    Concepts in Practice \(\PageIndex{8}\): String concatenation

    Which produces the string "10"?

    1. 1 + 0
    2. "1 + 0"
    3. "1" + "0"
    Answer

    c. The 1 and 0 are enclosed in quotes indicating valid strings, and the + is not, indicating a concatenation operation. Thus, the string "10" is produced. Note that concatenation operator does not include any default separator characters like the space character.

    Concepts in Practice \(\PageIndex{9}\): String concatenation

    Which produces the string "Awake"?

    1. "wake" + "A"
    2. "A + wake"
    3. "A" + "wake"
    Answer

    c. "A" and "wake" are valid strings, and the + is not enclosed in quotes, indicating a concatenation operation. Thus the string "Awake" is produced. Note that the concatenation operator does not include any default separator characters like the space character.

    Concepts in Practice \(\PageIndex{10}\): String concatenation

    A user enters "red" after the following line of code.

    color = input("What is your favorite color?")

    Which produces the output "Your favorite color is red!"?

    1. print("Your favorite color is " + color + !)
    2. print("Your favorite color is " + "color" + "!")
    3. print("Your favorite color is " + color + "!")
    Answer

    c. Note that space characters must be explicitly included when using the concatenation operator.

    Concepts in Practice \(\PageIndex{11}\): String concatenation

    Which of the following assigns "one-sided" to the variable holiday?

    1. holiday = "one" + "sided"
    2. holiday = one-sided
    3. holiday = "one-" + "sided"
    Answer

    c. A concatenation of the two strings is assigned to the variable holiday. Note that "one-sided" would assign the same string to holiday.

    Try It: Name length

    Write a program that asks the user to input their first and last name separately. Use the following prompts (example input in bold):

      What is your first name? Alan
      What is your last name? Turing

    The program should then output the length of each name. Based on the example input above, the output would be:

      Your first name is 4 letters long
      Your last name is 6 letters long
    
    Interactive Code
     
     
    Answer

    first = input("What is your first name? ")
    last = input("What is your last name? ")
    print("Your first name is", len(first), "letters long")
    print("Your last name is", len(last), "letters long")

    Try It: Punctuation

    Write a Python computer program that:

    • Assigns the string "Freda" to a variable, name.
    • Assigns the string "happy" to a variable, feel.
    • Prints the string "Hi Freda!" with a single print() function using the variable name.
    • Prints the string "I'm glad you feel happy." with a single print() function using the variable feel.
    Interactive Code
    # Type your code after this comment
     
    Answer

    name = "Freda"
    feel = "happy"
    print("Hi,", name + "!")
    print("I'm glad you feel", feel + ".")


    This page titled 1.4: String Basics is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.