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

Other Useful Methods

This example program introduces four more useful string methods: replace(), upper(), lower(), and split().  

Example script: other_methods_intro

  • Enter, name, and save other_methods_intro.  
  • Click the Send to micro:bit button.
# other_methods_intro

from microbit import *

sleep(1000)

string = "This is a string.  It is a sequence of characters!"
print("Original string:", string)
print()

lower_case = string.lower()
upper_case = string.upper()

print("lower_case =", lower_case)
print("upper_case =", upper_case)
print()

new_string = string.replace("It", "A string")

print("new_string =", new_string)
print()

new_list = string.split()

print("new list =", new_list)
print()
  • Check the results in the serial monitor.
  • Verify that it displays the original string, the lower and uppercase versions, a version that replaced “It” with “A string”, and a list with all the substrings that were separated by spaces in the original.

How other_methods_intro Works

The first routine declares a string, names it string, and then prints it for reference.

string = "This is a string.  It is a sequence of characters!"
print("Original string:", string)
print()

The string.lower() and string.upper() methods return lower and upper case versions of the original string, and print them:

lower_case = string.lower()
upper_case = string.upper()

print("lower_case =", lower_case)
print("upper_case =", upper_case)
print()

The string.replace() method is another way to replace parts of a string.  It’s not as straightforward as the approach from the previous String Surgery section, because it would replace all instances that matche the substring, if there were more than one “It”.  

new_string = string.replace("It", "A string")

print("new_string =", new_string)
print()

The string.split() method splits a string with separators into substrings.  By default, the separator is a space, but you could pass it a comma, for example.  Many data strings are comma delimited, and need to be split before each individual data item can be examined.

new_list = string.split()

print("new list =", new_list)
print()

 


Printer-friendly version
Your Turn: Exact Match vs Found in String
Prev
Try This: Accept in Any Case
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress