About Strings and Characters

A string is a sequence of characters.  You are likely to see strings expressed in many different ways in Python scripts, so this page shows many of the formats you are likely to encounter.

It also introduces how strings can contain data.  Because of this, Python has many features for manipulating strings.  Examples include indexing, built-in functions, and string methods.

Many Options for Creating Strings

Strings can be enclosed in

  • apostrophe single quotes: 'hello'
  • double quotes "hello"
  • triple apostrophes '''hello'''
  • three double quotes """hello"""

Strings with multiple lines have to be enclosed in either three single quotes or three double quotes. 

Keep in mind that an apostrophe and a single quote are the same thing.  But, two apostrophes '' are not the same as a double quote ".  A double quote is inserted when you hold the Shift key as you type an apostrophe key.  

'This is a string, enclosed by single-quotes.'

"This is also a string, enclosed by double-quotes."

'''This is also a string
 with more than one line
 enclosed by three single-quotes.'''

"""This is also a string
 with more than one line
 enclosed by three double-quotes."""

 

A single-line string can span multiple lines of code if each line is joined by the backslash \ character.  One advantage to double quotes is that those strings can contain apostrophes.

"This is a string with only one line"\
"that has been split into multiple"\
"lines to fit in your code editor"\

"This string in double-quotes 'contains' single-quotes."

 

Strings can also contain special characters called escape characters.  In general, escape characters are preceded by a backslash \ and are used to insert characters into a string that you cannot use just by typing them. 

For example, a string enclosed by single quotes could not normally contain a single quote, but preceding it with a backslash, like this: \' makes it possible.  This string also has an embedded tab, single quote, and new-line:

'\t Tab, \' apostrophe, \n...and text on a second line.'

 

A single character, like an  'A' in quotes is a string object, even though all it contains is one character.  Scripts sometimes start with an empty string that they add characters or strings to later.  There are four examples of this empty string after the 'A' character. 

'A'
''
""
''''''
""""""

 

With or Without a Variable

Strings do not necessarily have to be assigned to variables.  For example, here is a string in a print statement that uses a string as-is:

print("This is a string without a variable reference.")

 

In many cases, it is better to name a string.  If you use a long string more than once, naming it will save considerable code space.  For example, instead of two print statements, a string is named once and printed twice:

 s = "This is a string with a variable references: s."
print(s)
print(s)

 

ASCII Codes

Inside the micro:bit, characters are actually stored as numbers.  In the case of the character A, it's number is 65.  The number for the B character is 66.  The numbers are called ASCII codes, and are actually the numbers your keyboard sends your computer when you type the A, B, and other keys.  Writing scripts that manipulate ASCII codes is a first step toward encrypting messages for cybersecurity.

You can find the ASCII Table for codes 0 to 127 in the Reference section:

ASCII Table 0–127