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

First String

Let’s start with a script that creates a string, names it s, and prints it.

Example script: first_string_intro

  • Connect your micro:bit module to your computer with its USB cable.
  • In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
  • Enter, name, and save first_string_intro.
    (See Save & Edit Scripts.)  
  • Click Send to micro:bit.
    (See Flash Scripts with Python Editor.)
# first_string_intro

from microbit import *

sleep(1000)

s = "ABC 123"
print(s)
  • Click Show serial to open the serial monitor.
    (See Use the Serial Monitor.)
  • Verify that it displays ABC 123.

How first_string_intro Works

The statement s = “ABC 123” creates a string variable named s.  The string it refers to contains the characters A B C  1 2 3.  The statement print(s) displays the contents of the string named s.


Did You Know?

A print statement allows you to display more than one string at a time.  You will find it helpful to add a string that explains what’s about to be printed.  So, instead of just print(s), you could use print(“s = “, s).  This will become important when you write larger scripts.  While adjusting them to work the way you want, you can include information such as the variable name and the location within the the script the print statement is being executed.  Here is an example:

s = "ABC 123"
print("s =", s)

 

As mentioned earlier, strings can contain multiple lines.

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

 

A single line string can also be split up into multiple lines in the Python script.

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

 

A string in double quotes can contain single quotes:

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

 

Strings can also contain escape characters preceded by a backslash to print special-case characters like the tab, apostrophe, and newline.  Here’s an example you will try.  

s4 = 's4 has tab \t apostrophe \', and newline \n...for next line.'

Keep in mind that if the above string was enclosed in double quotes, it would not need the \’ to display the apostrophe.

 


Printer-friendly version
String Features: Indexing, Functions, Methods
Prev
Try This: Display with Variable Labels
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress