A string is a sequence of characters. You are likely to see strings expressed in many different ways in Python scripts, so this page shows many of the formats you are likely to encounter.
It also introduces how strings can contain data. Because of this, Python has many features for manipulating strings. Examples include indexing, built-in functions, and string methods.
Strings can be enclosed in
Strings with multiple lines have to be enclosed in either three single quotes or three double quotes.
Keep in mind that an apostrophe and a single quote are the same thing. But, two apostrophes '' are not the same as a double quote ". A double quote is inserted when you hold the Shift key as you type an apostrophe key.
'This is a string, enclosed by single-quotes.' "This is also a string, enclosed by double-quotes." '''This is also a string with more than one line enclosed by three single-quotes.''' """This is also a string with more than one line enclosed by three double-quotes."""
A single-line string can span multiple lines of code if each line is joined by the backslash \ character. One advantage to double quotes is that those strings can contain apostrophes.
"This is a string with only one line"\ "that has been split into multiple"\ "lines to fit in your code editor"\ "This string in double-quotes 'contains' single-quotes."
Strings can also contain special characters called escape characters. In general, escape characters are preceded by a backslash \ and are used to insert characters into a string that you cannot use just by typing them.
For example, a string enclosed by single quotes could not normally contain a single quote, but preceding it with a backslash, like this: \' makes it possible. This string also has an embedded tab, single quote, and new-line:
'\t Tab, \' apostrophe, \n...and text on a second line.'
A single character, like an 'A' in quotes is a string object, even though all it contains is one character. Scripts sometimes start with an empty string that they add characters or strings to later. There are four examples of this empty string after the 'A' character.
'A' '' "" '''''' """"""
Strings do not necessarily have to be assigned to variables. For example, here is a string in a print statement that uses a string as-is:
print("This is a string without a variable reference.")
In many cases, it is better to name a string. If you use a long string more than once, naming it will save considerable code space. For example, instead of two print statements, a string is named once and printed twice:
s = "This is a string with a variable references: s." print(s) print(s)
Inside the micro:bit, characters are actually stored as numbers. In the case of the character A, it's number is 65. The number for the B character is 66. The numbers are called ASCII codes, and are actually the numbers your keyboard sends your computer when you type the A, B, and other keys. Writing scripts that manipulate ASCII codes is a first step toward encrypting messages for cybersecurity.
You can find the ASCII Table for codes 0 to 127 in the Reference section:
Strings can contain characters that represent numbers. It is important to understand that a string with "1234" is very different from an int with a value of 1234 that you can add, subtract, multiply, etc. Instead, "1234" are just the characters that represent the numbers.
For example, take a string variable named s that contains the characters "1234". The quotes make it a string. Without the quotes, n = 1234 creates an int variable that will work in calculations.
s = "1234" # this makes a string with characters n = 1234 # this makes an int variable with the value 1234
Radio and Internet devices often have to convert numbers to strings and add names before sending them to other devices. The devices that receive the string have to find the names and numbers in the string. In many cases, they also have to convert the string representation of the number back to an int or float so that it can be used in calculations. For example, here are two names and numbers:
s1 = "First number" n1 = 1234 s2 = "Second number" n2 = 5678
One script would have to assemble or packetize the string into this before transmitting:
s = "First number 1234 Second number 5678"
The script running in the receiver might have to parse this data. In this case, parsing would involve breaking the string into its four substrings: "First number", "1234", "Second number", and "5678". It might also be up to the receiver to find the "1234" and "5678" substrings and convert them back to int variables that the script can use for calculations.
Sending and receiving scripts might also have to encrypt the string so that nobody other receivers cannot figure out what the string contains. Here is a very simple example of an encrypted string. The transmitter would have to change each character in some way, and the receiver would have to change each character back to the original before it can process the data. Can you decrypt it and figure out what it really says?
s = "Ifmmp"
In Python, strings can also contain Python statements. For example, this string:
s = """ print("Hello!") print("Hello again!") """
...can be sent from one micro:bit to another. The receiving micro:bit can actually execute the statements inside the string. This resembles how servers send code to computers to be executed in web pages. The web content and Javascript to respond when you click buttons are all sent from the server, to your client computer, and then executed by your browser.
Each character in a string has an index number. That index starts at the first character, counting from zero.
For example, in s = "Hello string", H has an index of 0, e has an index of 1, l has an index of 2, and so on, up through g with an index of 11. See how even the space between Hello and string is a character with an index?
Python has many tools for accessing, manipulating, and converting string information. Here is one indexing example where the 6th character 's' is copied to the c variable and then printed.
Python has built-in functions for certain string operations:
Additionally, strings that represent other data types can be converted to those types with built-in functions like int(), float(), dict(), list() and more. Example:
s = "123" # A string with numbers, but cannot be used in calculations n = int(s) # Convert to an int type n = n + 1 # The result can be used in calculations
The string class has many methods. Some return information about the string. Others return new strings that are changed versions of the originals, and there are also some that return different types of objects that contain the string’s characters. A few examples are shown below.
string = "This is a string. It is a sequence of characters!" char_index = string.find("sequence") # returns index of first letter in sequence new_string = string.replace("It", "A string") # replaces "It" with "A string" char_list = list(string) # converts a string into a list lower_case = string.lower() # converts string to all lower case upper_case = string.upper() # converts string to all upper case
Links
[1] https://learn.parallax.com/support/reference/ascii-table-0-127