Before looking at Processing’s functions for drawing text, an introduction to strings is required. This section covers some essential Python string operations.
In programming terminology, text is referred to as string data. More correctly, one could refer to a string as a series of characters. You’ve already encountered this data type in lesson 1, and know that strings are to be wrapped in quotation marks. One may use single- or double-quotes, but always ensure that you close-off using the same type with which you opened.
Create a new sketch and save it as “strings”. Add the following code to get started:
Add another string variable:
... whatsup = 'what's up!'
Because of the apostrophe in
what's, the string is closed before the
s, leaving a dangling third quote with no counterpart. Run the sketch and observe the error message:
There are a few ways to fix this. One can opt for double quotation marks:
whatsup = "what's up!"
or escape the apostrophe character using a backslash:
whatsup = 'what\'s up!
whatsup variable, and add another line using nested single- and double-quotes:
Concatenation and Formatting
+ operator performs arithmetic addition on numbers (integers and floats). However, it becomes a concatenation operator when provided string operands. To concatenate means to connect together, or link, in a series or chain. Try this example:
This displays the following line in the Console:
hello worldwhat's up!is your name really "world"?
Note how concatenating joins strings together exactly as they are defined, with no additional space characters. Spaces – along with any other punctuation you desire – must be explicitly included. Edit your code:
The Console now displays:
hello world. what's up! is your name really "world"?
An alternative to concatenating is string formatting, for which Python provides the
% operator. This works by substituting placeholder symbols with the relevant string values, as opposed to chaining them together in a sequence with other characters. As an example, here’s the same line constructed using the
all = ('%s. %s %s') % (hello, whatsup, question)
This approach has its advantages, but for this lesson, we’ll stick to the concatenate operator (
+). For more on this string formatting, consult the reference.
What follows below are descriptions for several string manipulation functions and methods, along with some code that you can add to your working sketch. Feel free to experiment with the arguments to see how things respond. Each demonstration acts on the
all variable which, to restate, now represents:
hello world. what's up! is your name really "world"?
length function returns the total number of characters of any string within the parentheses.
Python slice notation (
) provides a simple, yet powerful means of extracting characters from strings. Add this basic example to your code:
The position (index) of the character you intend to retrieve is placed within the square brackets. Take note that the indexing system is zero-based, meaning that the character indices begin at
0 (and not
A colon (
:) can be used to specify a range of characters. It operates in a few different ways. Add a number to the right of it, and it will return all of the characters up to but not including the specified index:
Add numbers to both the left and right of the colon, and it will return all of the characters beginning at the left index up to, but not including, the right:
A single value to the left of the colon returns everything from the given index to the end of the string:
You may also include negative values:
There are a few other ways in which the colon operator can slice strings, but these should be sufficient for now.
You’ll reencounter this notation in future lessons dealing with lists and dictionaries.
A Python method looks and behaves much like a function. With no knowledge of object-oriented programming, it’s difficult to explain exactly why methods are methods. However, all that you need to understand for now is the syntactical differences between the two, i.e. how you write a method versus a function. To contrast the two approaches – take the length function as an example:
Were the length function a method, it would be instead be written as:
len() is not a method, so this would result in an error. What is important to note, however, is how the method begins with a period (
.) and is appended to the variable.
What follows below are descriptions for several string methods, along with some code to add to your working sketch. Each example builds on the code before it, so you’ll need to work through all of them, entering each line as you progress.
Returns a version of the string with all lowercase characters converted to uppercase.
Returns a version of the string in title case (the first letter of each word in uppercase).
Returns the total of times the character/character-sequence appears in the given string.
Returns the index of where the term (first) appears in the string. If the substring is not found, returns
If the term appears multiple times, one can provide a second argument indicating the index from which the search should begin:
A third argument can be provided to indicate where along the string the search terminates.
Time for a challenge!
Using just the
all variable, produce a Console output that reads:
Hello. What is your name?
To start you off, here’s a snippet of the solution:
To successfully complete the task, you’ll need to combine various string methods.