Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Strings & Characters Primer

Strings & Characters Primer

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

 


Printer-friendly version
Strings for Exchanging Data
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress