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
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:
- Indexing from the left hand side for start begins at 0.
- 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
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'
|
!= | Comparison operator tests whether two values are not equal s1 = 'ABC'
|
in | Membership operator s1 = 'ABCD'
|
not in | Membership operator s1 = 'ABCD'
|
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:
Function | Description |
---|---|
chr()[1] | Converts an integer code to a character chr(65) ⇒ 'A'
|
ord() | Converts a character to an integer code ord('A') ⇒ 65
|
len() | Returns the length of a string len('Test string') ⇒ 11
|
str() | Returns a string representation of an object str(32 + 5) ⇒ '37'
|
sorted(item [, reverse = True|False]) |
Returns string as a sorted list string1 = 'dag'
|
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
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
Method | Description |
---|---|
str.count(sub [, start [, end]]) |
Counts instances of a substring s1 = 'spam ham am'
|
str.endswith(sub) | Returns True if string ends with the substring s1 = 'spam ham am'
|
str.startswith(sub) | Returns True if string starts with the substring s1 = 'spam ham'
|
str.find(sub
[, start [, end]]) |
Finds first occurrence of substring. Returns -1 if not found s1 = 'spam ham am'
|
str.rfind(sub
[, start [, end]]) |
Finds last occurrence of substring. Returns -1 if not found. s1 = 'spam ham am'
|
str.index(sub
[, start [, end]]) |
Finds first occurrence of substring. Raises an exception if the value is not found. s1 = 'spam ham am'
|
tr.rindex(sub
|
Finds last occurrence of substring. Raises an exception if the value is not found. s1 = 'spam ham am'
|
Character Classification
Method | Description |
---|---|
str.isalpha() | Checks if all characters in string are letters 'spam ham clam'.isalpha()
|
str.isdigit() | Checks if all characters in string are digits 'spam ham clam'.isdigit() ⇒ False
|
str.isspace() | Checks if all characters in string are whitespace ' '.isspace() ⇒ True
|
str.islower() | Checks if all letter characters in string are lowercase 'spam ham clam 789'.islower() ⇒ True
|
str.isupper() | Checks if all letter characters in string are uppercase s1 = 'SPAM HAM CLAM JAM 789'
s2 = 'Spam Ham clam jam 789'
'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.
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()
|
str.rstrip(chars) | Removes characters from the right, based on the argument. If no argument is given, whitespace is removed.
'SPAM 789 '.rstrip()
|
str.strip(chars) | Combined result of applying lstrip and rstrip methods. If no argument is given, whitespace is removed ' SPAM - 789 '.strip()
|
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')
s1 = 'abcabdabe'
|
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']
|
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. s1 = 'AB CD EF'
s2 = 'A%B%C'
|
str.rsplit([sep,
count]) |
Same as split but operates from right hand side of the string s1 = 'AB CD EF'
s2 = '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