Variables in Python 3 – Part 2: Text

Introduction

Lets continue with the idea of a video game that we introduced in the first part of this article. Imagine your player has a name. This could be hard coded, but what if the player gets to pick the name, in which case it is dynamic. This would be a case where a text variable is needed!

A variable that contains text (a sequence of letters and numbers that have been “strung” together) is called a string.

In this post we will learn how to represent strings in Python, how to use them as variables, and how to combine two or more strings together.

Strings in Python

When we are working in a Word document, or an email, we can type text directly and the program knows it is text data. In Python this is slightly different. Because Python has its own special syntax (language structure) that tells the computer how to behave, directly typing text in won’t work. How will the Python interpreter tell what is a command and what is text data?

To indicate text data, we need to block it out using quotation marks. For this, we can use single or double quotes.

Create a new file, called strings_part1.py and place the following code in it:

Run your program and you will get the following:

Just like with number variables, we can take these hard-coded strings (called ‘string literals’) and place them into string variables. The syntax for a string variable is just like for a number variable. We can name it however we want, and we use the assignment operator (= sign) to set the value.

Create a new file, called strings_part2.py and write the following:

When you run this program (python strings_part2.py), you will see the same output as before. The only difference is that this time, internally, Python printed the names from string variables instead of string literals.

Combining Strings

Sometimes you might want to combine multiple strings together. For example, maybe you are interested in combining a first name and a last name to display the full name of your player. There are a few ways to combine strings in Python.

Concatenation

One method of combining strings is simply adding them together. This is called string concatenation. Concatenation can be performed both on string literals and on string variables, or on a combination of the two.

For example, save the following program as strings_part3.py and run it:

You will see the following printed to your screen: Mr. John Smith

We have concatenated two string literals with two string variables to produce the final output. When you concatenate strings, the pieces are stacked onto each other in the order you wrote it. This is the simplest way of combining strings, but when you are building complex messages it can become very messy to concatenate in this manner.

Interpolation

String interpolation is another method of combining strings together to create a more complex message. With interpolation, you write out the entire message and use wildcards to indicate the placeholders that will be filled in dynamically.

Python 3 gives you two methods of performing string interpolation. Try the following example, strings_part4.py:

When you run the program, you should get the following:

Mr. John Smith
Ms. Jane Doe

In both cases we are using the string format function to perform the interpolation. In the first example, the placeholders are replaced in the order that you add them in the format argument list. In the second example, we are using named placeholders. So, we could provide them in any order to the format function. For example, add the following line to strings_part4.py, and run it again:

When you run the program again, you should see:

Mr. John Smith
Ms. Jane Doe
Mr. Smith John
Ms. Jane Doe

Notice the name in the first example is now in the wrong order (Mr. Smith John) while the name in the second example is still correct (Ms. Jane Doe).

Maybe you noticed the lines beginning with the hash sign (#). Programmers often need to leave notes in their code to remind them or other programmers what the code does. These are called comments, and are ignored by the Python interpreter. Any line you want to mark as a comment must begin with the hash sign.

Conclusion

You should not be pretty comfortable storing and print text data in your Python program. String literals and string variables can both be used to print out data. You can build complex messages by concatenating and interpolating strings.

In the next lesson we will start looking at how to capture input from the user, both as numbers and text.