String Variables

In a previous section, we stored numbers inside of our variables. But variables can be used to store strings of text as well. You do so in the same way. This time, however, you need to surround whatever is to the right of the equals sign with quotation marks. You can either use double quotes:

myText = "Some text here"

Or you can use single quotes:

myText = 'Some text here'

But you can't mix the two. This would get you an error:

myText = "Some text here'

The line above uses a double quote at the start of the text and a single quote at the end.

Let's display some text in our message box. Just before your message box line, add the following:

myText = "I have a dream."

We now need to place this variable and its stored text into the message box.

Instead of typing "Title Here" and "Message Here" you can replace the direct text with variables. Amend you message box to this:

messagebox.showinfo("Title Here", myText)

Version 2 of Python may need a tk before MessageBox:

tkMessageBox.showinfo("Title Here", myText)

Notice that older Python versions also need capital letters: tkMessageBox, not tkmessagebox.

So here's what your code should look like for version 3 of Python:

Python String variables

And here it is for version 2:

String variable set up in Python

(The image above is from a Mac. If you're not using a Mac, delete the underscore character on the first line. If import tkinter is greyed out, as in the image above, it means you don't need the line at all and can delete it.)

Run your code to see your message:

Tkinter message box with Python string variables

You can change the title in the same way. Add another variable to your code, just below the first one:

myTitle = "inspiration"

Now replace "Title Here" with the name of your variable:

messagebox.showinfo(myTitle, myText)

Your code should be this in version 3 of Python:

from tkinter import messagebox
myText = "I have a dream."
myTitle = "Inspiration"
messagebox.showinfo(myTitle, myText)

Or this, in version 2:

import tkMessageBox
myText = "I have a dream."
myTitle = "Inspiration"
tkMessageBox.showinfo(myTitle, myText)

Run your code to see your message displayed:

TKinter message box imported into Python

Python Exercise

Change the text inside of your two string variables. Run your code to get a new message.

Concatenation

You can join strings together. You do this with the plus symbol. For example:

person = "Martin Luther King"
personSaid = "I have a dream."
quote = person + personSaid

Here, we've set up three variables. The first two store some text. The third joins together the two strings of text. They are joined together (concatenated) with a plus symbol. You can join together as many string variables as you want. Just remember to add a plus symbol for each one. Try it out:

from tkinter import messagebox
myTitle = "Inspiration"
person = "Martin Luther King"
personSaid = "I have a dream."
quote = person + personSaid
messagebox.showinfo (myTitle, quote )

(If you're using Python 2, amend your code accordingly.)

Run your code and you'll see this:

Tkinter message box

Notice how there is no space between the person and what the person said. You can enter a blank space (or any other character) with some more plus symbols:

quote = person + " " + personSaid

The space goes between quote marks. On either side of the space we now have plus symbols. Miss one out and you'll get an error.

You could have a colon instead of a blank space:

quote = person + ": " + personSaid

You can even add the space or colon directly in the text for the variables:

person = "Martin Luther King: "

Or this:

personSaid = ": I have a dream."

The effect will be this, in the message box:

Tkinter message demonstrating Python concatentation

It's much more readable, now.

ASIDE

We said that you can't mix quotation marks. But that just applies to the start and end ones. There are times that you may actually need a single quote with two doubles. For example, some people's names include single quotes, like O'Reilly. You can include the single quote here between two doubles:

surname = "O'Reilly"

If you prefer single quotes for your strings of text, then this would get you an error:

surname = 'O'Reilly'

To solve the problem, you can use the escape character, which is a backslash (\). The escape character goes before the one you want to escape:

surname = 'O\'Reilly'

If you need to use the \ character in your string, then you can tell Python that this is raw text. You do this by putting a letter r before your text. For example:

file_path = r"C:\folder\subfolder"

Here, we actually need the \ characters as part of the file path. This is surrounded by double quotes. But before the first one, we've typed an r. Miss the r out and it would give you an error.

 

In the next lesson, we'll take a look at strings and index numbers. It's an important topic, so make sure you don't skip it!

Strings and Index Numbers >