Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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

String indexes

1.
What is the index of the second character in a string?
  1. 1
  2. 2
  3. -2
2.
If s = "Python!", what is the value of s[1] + s[-1]?
  1. "P!"
  2. "y!"
  3. "yn"
3.
If s = "Python!", what type of object is s[0]?
  1. character
  2. integer
  3. string

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.

      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)     
    
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.

Concepts in Practice

ord() and chr()

4.
What is the code point for the letter A?
  1. 1
  2. 65
  3. 97
5.
What value does ord("0") return?
  1. 0
  2. 48
  3. Error
6.
What does chr(126) return?
  1. ~
  2. "~"
  3. Error

Special characters

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

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 \
Table 3.2 Common escape sequences.

Concepts in Practice

Tabs and newlines

7.
Which of the following is an escape sequence?
  1. t
  2. /t
  3. \t
8.
Which statement prints a backslash (\) to the screen?
  1. print(\\)
  2. print(\"\")
  3. print("\\")
9.
Which statement prints Enter and here on separate lines?
  1. print("Enter here")
  2. print("Enter" + \n + "here")
  3. print("Enter" + "\n" + "here")

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
    

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.

Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Mar 15, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.