Lesson Contents
Python strings are one of the Python data types. A string is a sequence of characters. In this lesson, you will learn how to create and manipulate strings in Python.
Double or Single Quotes
We create a string by enclosing characters inside single or double quotes. Here’s an example with double quotes:
And an example with single quotes:
Both work and “hello world” is the exact same thing as ‘hello world’. What’s the difference then? You could pick one of the two when you want to use a single or double quote within the string. Let me show you an example:
In the code above, we get an invalid syntax error. Why? Python thinks the string ends with the single quote after the word “router”. We can work around this by using double quotes for our string:
This solves our problem. We can also use this the other way around when we want to use a double quote within our string. For example:
When we switch to single quotes, we can use double quotes within our string:
Our string is now valid.
Escape character
Escaping a single or double quote within a string is possible with the examples above but we can also use the \ (backslash) as an escape character. Here is an example:
By using a \ before the character we want to escape, Python ignores the character and correctly prints the string. If you only need to escape a few characters, this is a good way to do it. If you have a string with many characters to escape, it might be easier to switch between single or double quotes to create your strings.
Triple Quote Multiline String
If you want a string that consists of more than one line, you can use triple quotes. Here is an example:
The output above looks clunky. Between the lines, you can see the \n symbol. Python interprets this as a new line. If you want the string to look nice, you should use the print command. Here is an example:
Because of the print command, Python processes the /n newline symbol and prints the second part of the string on a new line.
String Concatenation
In Python, we can concatenate (combine) strings with the + symbol. Here is a quick example:
This combines “Router ” and “R1” into “Router R1”. You can also multiply strings:
This shows the string multiple times.
Concatenation Errors
You can’t concatenate everything. When you try to concatenate a string and an integer, you get an error:
Python tells us that we can’t concatenate a string and an integer. Hmm, too bad. There are two ways to work around this. The first option is to represent our integer as a string:
This works, but it’s not always possible. What if the integer is returned from an external program? The data we receive is not always under our control.
Fortunately, Python can convert between different data types. With the str()
function, we can convert something into a string. Let’s convert our integer into a string:
This solves our problem.
String Formatting
We can use string formatting to insert values into a string. There are two options. Let me show you both.
% Operator
This is the older method where we use the % operator. Here’s how it works:
- The % within our string is a substitute.
- The % operator at the end of the string formats a variable we specify with our string.
Let me show you an example:
In the code above, I used the %s operator to convert the value into a string. You can also do this with multiple values but you have to add the values between parentheses:
This is how we use the % operator. It supports many more options but the examples above suffice for most simple scripts.
Format() Method
The format()
method is the newer way of inserting values into strings. Here’s how it works:
- Within the string we use {} as a placeholder.
- We append
.format()
to the end of the string and add the value we want to insert between the parentheses.
Here’s what it looks like:
We can also do this for multiple values like this:
F-strings
Available from Python 3.6, this is the easiest method to format your strings and if you are new to Python, it’s best to use this from now on. I will explain these in the next lesson.
Conclusion
You have now learned how to work with Python strings:
- You can use single or double-quoted strings to create a string.
- The \ (backslash) character can be used as an escape character.
- Triple quote multiline strings are for strings that consist of more than one line.
- We can concatenate strings with the + symbol.
- You can convert a data type into a string with the
str()
function. - There are three options to format your strings:
- The % operator.
- The
format()
method. - F-strings (explained in the next lesson).
I hope you enjoyed this lesson. If you have any questions feel free to leave a comment.