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

Try This: Access Substrings

In the previous activity, a single character was accessed using an index, like set c to (char in (s) at (3)). Your projects can also view segments of strings by using a range instead of a single index number in the square brackets. 

Let’s say you have a string named s:

  • char in (s) at (5) — fifth character in a string
  • substring of (s) from (3) of length (4) — third through sixth characters in a string
  • substring of (s) from (0) of length (5) — beginning through the fifth character
  • substring of (s) from (11) of length (length of (s)) — eleventh character through the end of the string.

In Makecode-speak, strings are considered immutable. Being immutable means that once created, a string cannot be changed. That doesn’t mean that s = “Have a nice day.” cannot be changed to s = “Have a GREAT day!” It just means that the original “Have a nice day.” string cannot be changed. A block can still grab parts of “Have a nice day.” and use them to create a new string that reads “Have a GREAT day!” The resulting string can even be assigned back to s.

The s variable starts as this string: “Have a nice day.” The second block adds “Have a ” + “GREAT” + ” day” + “!”. See how “Have a “ is s[:7] from the original string? Add the string “GREAT” to that, and then ” day” with a leading space, which is s[11:End], and lastly add “!”, and the string surgery is complete!

Example project: string_surgery_try_this

This project starts with “Have a nice day.” and then creates a new string sn with the “nice” portion of the original. After that, it demonstrates some more ways to access segments of an original string that were introduced above.

  • Enter and name string_surgery_try_this. 
  • Click the Download button.

  • Check the results in the serial terminal.
  • Verify that it displays:


Printer-friendly version

Leave a Comment Cancel reply

String Surgery
Prev
Your Turn: Create a Modified String
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress