MicroPython ‘string’ Data Type

Contents

Introduction

MicroPython is a “slim” version of Python specifically designed with a small footprint to efficiently run on memory constrained microcontrollers.

Computer languages have a built-in set of data types along with user capability to define custom types. MicroPython is no exception. Following are the MicroPython standard or built-in data types:

  • Numeric
  • Boolean
  • Sequence Type
  • Set
  • Dictionary
Python data type hierarchy
Source: geeksforgeeks.org

As can be seen above, strings are a part of the ‘sequence’ family types. The other sequence types being list and tuple.

String Operators

A string consists of none, one or more characters enclosed by either double or single quotes. The type of quote (either double or single) must be consistent for both opening and closing. This is best explained by example.

Strings are immutable. This means that once the string value is defined it cannot be altered or otherwise changed. This concept will become clearer with examples given in the section below on slicing.


'A' legal string
"Big Bad Boy" legal string
"Hello there' illegal, cannot mix quote types
'' legal, this is known as an empty string
          

Assignment

A string value can be assigned to a variable using the = assignment operator.


s1 = 'naughty boy @#!' assigned to a variable
s2 = "4 + 9" assigned to a variable
s3 = ''  empty string
            

Slicing

Any single character within the string can be referenced using simple indexing; string[index]. Note that a MicroPython index always starts at 0 unless otherwise user defined. This applies to strings.


'ABCDEFG'[0]  = 'A' returns first character of the string.
'ABCDEFG'[1]  = 'B' returns second character of the string.

s1 = 'naughty boy @#!'
s1[50]  error - IndexError: str index out of range.
            

While this is useful, it gets much more clever. Multi-character substrings can be extracted with a simple extension of this syntax using the slicing operator : in the form string[start : end]. There are two important points to this syntax to note:

  1. Indexing from the left hand side for start begins at 0.
  2. The character with the end index is not returned.

s1 = 'ABCDEFG'
s1[0 : 3]  = 'ABC'
s1[2 : 6]  = 'CDEF'
            

So far, index counting has been done from left to right. It is also possible to extract a substring by counting from right to left. In this case the starting index is -1


s1 = 'ABCDEFG'
s1[-2]  = 'F'
s1[-3 : -1]  = 'EF'
s1[2 : -2]  = 'CDE'
            

Strings are immutable. This means that a given string cannot be changed. The following will give an error:

my_string = 'ABC'
my_string[1] = 'D'
  TypeError: 'str' object doesn't support item assignment
            

More String Operators

Table 1: MicroPython string Operators
Operator Description
+

Concatenation; joins two or more strings.

('A' + 'B' + 'C') ⇒ 'ABC'

*

Repeats a string a given number of times.

('ABC' * 3) ⇒ 'ABCABCABC'

==

Comparison operator tests whether two values are equal

s1 = 'ABC'
s2 = 'ABC'
s1 == s2 ⇒ True

!=

Comparison operator tests whether two values are not equal

s1 = 'ABC'
s2 = 'CBA'
s1 != s2 ⇒ True

in

Membership operator

s1 = 'ABCD'
'BC' in s1 ⇒ True

not in

Membership operator

s1 = 'ABCD'
'bc' not in s1 ⇒ True

Built-In String Functions

Python provides many functions that are built into the interpreter and always available. The following work with strings and character data:

Table 2: MicroPython (micro:bit) - Built-In String Functions
Function Description
chr()[1]

Converts an integer code to a character

chr(65) ⇒ 'A'
chr(97) ⇒ 'a'
chr(8364) ⇒ ValueError:

ord()

Converts a character to an integer code

ord('A') ⇒ 65
ord('a') ⇒ 97
ord('€') ⇒ TypeError:

len()

Returns the length of a string

len('Test string') ⇒ 11
len('') ⇒ 0

str()

Returns a string representation of an object

str(32 + 5) ⇒ '37'
str(32) + str(5) ⇒ '325'

sorted(item
      [, reverse =
      True|False])

Returns string as a sorted list

string1 = 'dag'
sorted(string1) ⇒ ['a', 'd', 'g']
sorted(string1, reverse = True)
⇒ ['g', 'd', 'a']

String Methods

From Built-In String Methods: Overview by Christopher Bailey at realpython.com.

Methods are similar to functions. A method is a specialized type of callable procedure that is tightly associated with an object. Like a function, a method is called to perform a distinct task. But it is invoked on a specific object and has knowledge of its target object during execution.”

This series on MicroPython discusses classes and methods here.

Methods are invoked using dot notation i.e. String.Method() with the following tables providing simple examples. The tables below provides an exhaustive list of the MicroPython string methods.

Case Conversion

Table 3: MicroPython Case Conversion String Methods
Method Description
str.lower()

Converts to lower case

'TrEe 78'.lower() ⇒ 'tree 78'

str.upper()

Converts to upper case

'tReE 78'.upper() ⇒ 'TREE 78'

Find and Seek

Table 4: MicroPython Find and Seek String Methods
Method Description
str.count(sub
      [, start
      [, end]])

Counts instances of a substring

s1 = 'spam ham am'
s1.count('am') ⇒ 3
s1.count('am', 1, 5)
⇒ 1

str.endswith(sub)

Returns True if string ends with the substring

s1 = 'spam ham am'
s1.endswith('am')
⇒ True

str.startswith(sub)

Returns True if string starts with the substring

s1 = 'spam ham'
s1.startswith('spam')
⇒ True

str.find(sub
      [, start
      [, end]])

Finds first occurrence of substring. Returns -1 if not found

s1 = 'spam ham am'
s1.find('am')
⇒ 2
s1.find('xx')
⇒ -1
s1.find('am',3,8)
⇒ 6

str.rfind(sub
      [, start
      [, end]])

Finds last occurrence of substring. Returns -1 if not found.

s1 = 'spam ham am'
s1.rfind('am')
⇒ 9
s1.rfind('am',3,8)
⇒ 6

str.index(sub
      [, start
      [, end]])

Finds first occurrence of substring. Raises an exception if the value is not found.

s1 = 'spam ham am'
s1.index('am') ⇒ 2
s1.index('xx')
⇒ substring not found
s1.index('am',5,12) ⇒ 6

tr.rindex(sub
      [, start
      [, end]])

Finds last occurrence of substring. Raises an exception if the value is not found.

s1 = 'spam ham am'
s1.rindex('am') ⇒ 9
s1.rindex('xx')
⇒ substring not found
s1.rindex('am',5,8) ⇒ 6

Character Classification

Table 5: MicroPython Character Classification Methods
Method Description
str.isalpha()

Checks if all characters in string are letters

'spam ham clam'.isalpha()
⇒ False
'spamhamclam'.isalpha()
⇒ True

str.isdigit()

Checks if all characters in string are digits

'spam ham clam'.isdigit() ⇒ False
'123'.isdigit() ⇒ True
'123.5'.isdigit() ⇒ False

str.isspace()

Checks if all characters in string are whitespace

' '.isspace() ⇒ True
'A1B2 C3'.isspace() ⇒ False
''.isspace() ⇒ False

str.islower()

Checks if all letter characters in string are lowercase

'spam ham clam 789'.islower() ⇒ True
'Spam Ham clam 789'.islower() ⇒ False
''.islower() ⇒ False

str.isupper()

Checks if all letter characters in string are uppercase

s1 = 'SPAM HAM CLAM JAM 789'
s1.isupper() ⇒ True

s2 = 'Spam Ham clam jam 789'
s2.isupper() ⇒ False

'5'.isupper() ⇒ False

String Formatting

In addition to the methods described in Table 6 below, MicroPython also provides means to format strings that are being printed. This is through the interpolation operator % and the function format(). More details can be found here.

Table 6: MicroPython String Formatting Methods
Method Description
str.lstrip(chars)

Removes characters from the left, based on the argument. If no argument is given, whitespace is removed

' SPAM 789'.lstrip()
⇒ 'SPAM 789'
'spam-ham-789'.lstrip('spam')
⇒ '-ham-789'

str.rstrip(chars) Removes characters from the right, based on the argument. If no argument is given, whitespace is removed.

'SPAM 789 '.rstrip()
⇒ 'SPAM 789'
'spam-ham-789'.rstrip('89')
⇒ 'spam-ham-7'

str.strip(chars)

Combined result of applying lstrip and rstrip methods. If no argument is given, whitespace is removed

' SPAM - 789 '.strip()
⇒ 'SPAM - 789'
'89spam789'.strip('89')
⇒ 'spam7'

str.replace(old,
     new[, count])

Replaces matching occurrences of old with new.

Optional count specifies number of times to do the replacement.

'abcd'.replace('abc','ABC')
⇒ ''ABCd''

s1 = 'abcabdabe'
s1.replace('ab','AB', 2)
⇒ 'ABcABdabe'

Converting Between Strings and Lists

Table 7: Converting Between Strings and Lists
Method Description
str.join(iterable)

Returns a string by joining all the elements of a list, separated by the separator string

list1 = ['Big', 'bad', 'John']
' '.join(list1) ⇒ 'Big bad John'
'$'.join(list1) ⇒ 'Big$bad$John'

str.split([sep,count])

Breaks up a string at the specified separator and returns a list of strings

Whitespace is default if sep isn't specified.
Optional count specifies number of times to split the string

s1 = 'AB CD EF'
s1.split(' ') ⇒ ['AB', 'CD', 'EF']
s1.split(' ', 1) ⇒ ['AB', 'CD EF']

s2 = 'A%B%C'
s2.split('%') ⇒ ['A', 'B', 'C']

str.rsplit([sep,
     count])

Same as split but operates from right hand side of the string

s1 = 'AB CD EF'
s1.rsplit(' ')
⇒ ['AB', 'CD', 'EF']
s1.rsplit(' ', 1)
⇒ ['AB CD', 'EF']

s2 = 'A%B%C'
s2.rsplit('%')
⇒ ['A', 'B', 'C']


list1 = [1, 2, 3, 4, 5]
list1[2] = 6
 ⇒ list1 now equals [1, 2, 6, 4, 5]

tuple1 = (1, 2, 3, 4, 5)
tuple1[2] = 6
 ⇒ Error: 
  'tuple' object doesn't support item assignment