Operations on Strings

Finding String Length

To get the number of characters in a string, we use the Python function len. For example, len("Hello, World!") returns 13.

See how you can combine len() and [ ] notation to find information about a string:

String Length

Cutting Strings

Cutting out some part of a string gives you a substring. For example, the strings “eat” and “ted” are substrings of “repeated”. To extract a substring in Python, we use the syntax

S[ firstIndex : endIndex]

to get the substring starting at index firstIndex and ending at endIndex-1. Try to figure out the output of the following code before you run it.

Cutting Strings

Character Codes: ord, chr

As we mentioned in the introduction of this lesson, your computer actually represents every character as a number. Which number corresponds to which character? Generally, it can depend on which encoding your computer uses, but nearly all modern computers have a standard set of characters for the numbers between 32 and 255. Here is a list of the characters with numbers between 32 and 127:

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

It is not so useful to personally memorize the entire table, but there are some useful facts to remember:

  • the lowercase characters a, b, c, …, z have consecutive character codes
  • the uppercase characters A, B, C, …, Z have consecutive character codes
  • the digit characters 0, 1, 2, …, 9 have consecutive character codes

Character 32 is a space, while character 127 is one of several special “control” characters. Some useful control characters are 9, which is tab, and 10 and 13 which are used for newlines.

In Python, you can convert a character into its corresponding numerical code using the ord function. The chr function does the reverse: it takes a number as input, and returns the character with that code.

ord and chr

Coding Exercise

Write a program that takes a character as input (a string of length 1), which you should assume is an upper-case character; the output should be the next character in the alphabet. If the input is ‘Z’, your output should be ‘A’.

Coding Exercise Strings