Skip to main content
Engineering LibreTexts

3.1: Strings Revisited

  • Page ID
    117533
  • \( \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

    • Extract a specific character from a string using an index.
    • Use escape sequences to represent special characters.

    Indexes

    A string is a sequence of zero or more characters. Each character has an index that refers to the character's position. Indexes are numbered from left to right, starting at 0. Indexes are also numbered from right to left, starting at -1.

    Checkpoint: String indexes
    Concepts in Practice \(\PageIndex{1}\): String indexes

    What is the index of the second character in a string?

    1. 1
    2. 2
    3. -2
    Answer

    a. Since index values start at 0, the index 1 refers to the second character of the string.

    Concepts in Practice \(\PageIndex{2}\): String indexes

    If s = "Python!", what is the value of s[1] + s[-1]?

    1. "P!"
    2. "y!"
    3. "yn"
    Answer

    b. The character at index 1 is y, and the character at index -1 is !.

    Concepts in Practice \(\PageIndex{3}\): String indexes

    If s = "Python!", what type of object is s[0]?

    1. character
    2. integer
    3. string
    Answer

    c. The result of s[0] is "P" , which is a string of length 1.

    Unicode

    Python uses Unicode, the international standard for representing text on computers. Unicode defines a unique number, called a code point, for each possible character. Ex: "P" has the code point 80, and "!" has the code point 33.

    The built-in ord() function converts a character to a code point. Ex: ord("P") returns the integer 80. Similarly, the built-in chr() function converts a code point to a character. Ex: chr(33) returns the string "!".

    Unicode is an extension of ASCII, the American Standard Code for Information Interchange. Originally, ASCII defined only 128 code points, enough to support the English language. Unicode defines over one million code points and supports most of the world's written languages.

    Table 3.1 Character values. This table shows code points 32 to 127 as defined by ASCII and Unicode. Code points 0 to 31 are non-printable characters that were used for telecommunications.
          32  (space)  
          33  !
          34  "
          35  #
          36  $
          37  %
          38  &
          39  '
          40  (
          41  )
          42  *
          43  +
          44  ,
          45  -
          46  .
          47  /
          48  0
          49  1
          50  2
          51  3
          52  4
          53  5
          54  6
          55  7
          56  8
          57  9
          58  :
          59  ;
          60  <
          61  =
          62  >
          63  ?
            64  @        
            65  A
            66  B
            67  C
            68  D
            69  E
            70  F
            71  G
            72  H
            73  I
            74  J
            75  K
            76  L
            77  M
            78  N
            79  O
            80  P
            81  Q
            82  R
            83  S
            84  T
            85  U
            86  V
            87  W
            88  X
            89  Y
            90  Z
            91  [
            92  \
            93  ]
            94  ^
            95  _
          96  `
          97  a
          98  b
          99  c
         100  d
         101  e
         102  f
         103  g
         104  h
         105  i
         106  j
         107  k
         108  l
         109  m
         110  n
         111  o
         112  p
         113  q
         114  r
         115  s
         116  t
         117  u
         118  v
         119  w
         120  x
         121  y
         122  z
         123  {
         124  |
         125  }
         126  ~
         127  (delete)
    Concepts in Practice \(\PageIndex{4}\): ord() and chr()

    What is the code point for the letter A?

    1. 1
    2. 65
    3. 97
    Answer

    b. Code points 65 to 90 are used for uppercase letters.

    Concepts in Practice \(\PageIndex{5}\): ord() and chr()

    What value does ord("0") return?

    1. 0
    2. 48
    3. Error
    Answer

    b. The code point for the character "0" is 48.

    Concepts in Practice \(\PageIndex{6}\): ord() and chr()

    What does chr(126) return?

    1. ~
    2. "~"
    3. Error
    Answer

    b. Tilde (~) is the character for code point 126. The chr() function returns a string of length one.

    Special characters

    An escape sequence uses a backslash (\) to represent a special character within a string.

    Table 3.2 Common escape sequences.
    Escape sequence Meaning Example Screen output
    \n
    A newline character that indicates the end of a line of text.
    print("Escape\nsequence!")

    Escape
    sequence!

    \t
    A tab character; useful for indenting paragraphs or aligning text on multiple lines.
    print("Escape\tsequence!")
    Escape    sequence!
    \'
    A single quote; an alternative to enclosing the string in double quotes.
    print('I\'ll try my best!')
    I'll try my best
    \"
    A double quote; an alternative to enclosing the string in single quotes.
    print("I heard you said \"Yes\"")
    I heard you said "Yes"
    \\
    A backslash character.
    print("This prints a \\")
    This prints a \
    Concepts in Practice \(\PageIndex{7}\): Tabs and newlines

    Which of the following is an escape sequence?

    1. t
    2. /t
    3. \t
    Answer

    c. An escape sequence is two characters long and begins with a backslash (\).

    Concepts in Practice \(\PageIndex{8}\): Tabs and newlines

    Which statement prints a backslash (\) to the screen?

    1. print(\\)
    2. print(\"\")
    3. print("\\")
    Answer

    c. The first backslash indicates an escape sequence, and the second backslash indicates the character.

    Concepts in Practice \(\PageIndex{9}\): Tabs and newlines

    Which statement prints Enter and here on separate lines?

    1. print("Enter here")
    2. print("Enter" + \n + "here")
    3. print("Enter" + "\n" + "here")
    Answer

    c. The escape sequence \n produces a new line. print("Enter\nhere") produces the same result.

    Try It: Hopper quote

    Grace Hopper (1906–1992) was a famous computer scientist (and rear admiral in the US Navy!) who came up with the idea of machine-independent programming languages. She envisioned a programming language based on English and made many contributions that paved the way for modern programming languages, including Python.

    Write a program that prints the following text, including the quotation marks. Your program may not use single quotes (') anywhere in the code. The last line must be indented with a tab character.

        "To me programming is more than an important practical art.
        It is also a gigantic undertaking in the foundations of knowledge."
        	-- Grace Hopper
    Interactive Code
     
     
    Answer

    print("\"To me programming is more than an important practical art.")
    print("It is also a gigantic undertaking in the foundations of knowledge.\"")
    print("\t-- Grace Hopper")

    Try It: Shift cipher

    During the Roman Empire, Julius Caesar (100–44 BCE) used a simple technique to encrypt private messages. Each letter of the message was replaced with the third next letter of the alphabet. Ex: If the message was CAT, the C became F, the A became D, and the T became W, resulting in the message FDW. This technique is known as a shift cipher because each letter is shifted by some amount. In Caesar's case, the amount was three, but other amounts (besides 0) would work too.

    Write a program that prompts the user to input the following two values (example input in bold):

        Enter a 3-letter word: CAT
        Shift by how many letters? 3

    The program should then shift each letter of the word by the desired amount. Based on the example above, the output would be:

        The secret message is: FDW

    Hint: Use the ord() function to convert each letter to an integer, add the shift amount to each integer, use the chr() function to convert each integer to a character, and concatenate the resulting characters.

    Interactive Code
     
     
    Input

    CAT
    3

    Answer

    word = input("Enter a 3-letter word: ")
    shift = int(input("Shift by how many letters? "))

    a = chr(ord(word[0]) + shift)
    b = chr(ord(word[1]) + shift)
    c = chr(ord(word[2]) + shift)

    print("The secret message is:", a + b + c)


    This page titled 3.1: Strings Revisited 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.