Introducing the String Data Type
In this lesson, you’ll dive into what is a string. Previously, you might have created some programs in Python. And to start with, you’ve probably created the string
"Hello, World" and printed it out inside of an interactive session. In this lesson, you’ll get a deeper look into exactly what strings are and the various ways you can create them in Python.
00:23 Strings are one of the fundamental Python data types. The term data type refers to what kind of data a value represents. In the case of strings, they represent text.
00:34 There are several other data types built into Python, such as numeric ones like integers and floating-point numbers; Booleans; and sequence types, like a list or a tuple, which a string is actually part of. You’ll learn more about that later.
00:51 What does it mean to be a fundamental data type? It means that it can’t be broken up into smaller values of a different type. Those are actually called compound data types and are commonly known as data structures. Future Python Basics courses will dig into data structures.
The string data type has a special abbreviated name in Python,
str. To see it, you can use the built-in
type() function to show the data type of a given value.
Let’s check that out in an interactive window. Open up IDLE, and it should automatically create an interactive window at the triple-prompt, type out the word
You’ll see that it identifies itself as a function, and it’s expecting an object. In this case, we’re going to put a string inside of it. If you type, with quotation marks,
"Hello, World", it will show you the type.
The output is
<class 'str'> and indicates the value
"Hello, World" is an instance of the
str data type, and basically,
"Hello, World" is a string. For now, you can think of the word class as a synonym for data type, although it refers to something more specific, and you’ll get further into that in your Python Basics journey.
type() also works for values that you’ve assigned to a variable.
So create a variable named
phrase and then use the assignment operator (
=) and type a phrase into it. So maybe we just put
"Hello" this time. You can use
type() and place
phrase as an object inside of it.
And it knows that
phrase, the variable, is assigned to the string
02:43 Strings have three important properties: they contain individual letters or symbols called characters, they have a length defined as the number of characters that are contained within the string, and those characters within a string appear in a sequence.
02:59 Each character has a numbered position. It’s called an index. Here’s two examples of strings. As you can see, you can use single quotes or double quotes at the beginning and the end, as long as the same type is used at the beginning and the end of the string.
So in the case of
string1, you’re using single quotes around
'Hello, World'. In
string2, it’s using the double quotes around the numbers
"1234". Texts surrounded by quotation marks like this create a string literal.
03:32 The name literal indicates that a string is literally typed into your code.
03:39 String literals are written in a couple different ways. Again, you can use the single quotes, which allows you to embed double quotes within it, or you can use double quotes on the outsides at the beginning of the end, and that would allow single quotes on the inside.
03:55 Triple quotes is the third way you could possibly go, and they provide some additional value you’ll learn about shortly. Either three single quotes or three double quotes at the beginning and the end.
Strings can contain any valid Unicode character. So Unicode characters go beyond the simple letters of the alphabet that you’ve seen and include the symbols like an apostrophe (
') or the number symbol (
#), and other punctuation marks.
04:24 And they include numbers plus symbols that are involved in other languages that you might have seen, plus emoji. All of these are valid strings.
04:37 So when creating strings, such as the ones in the example, you can use single quotation marks,
04:47 or you can use double quotation marks. And again, you could put all types of different characters in them. If you were to try to assign with only a single quotation mark but then use a different type at the end, it will let you know that you’ve not created the string properly.
05:07 It’s not a proper string literal because it doesn’t start and end with the same type. The quotation marks surrounding the string are called delimiters. They tell Python where a string begins and where it ends. When one type of quotes is used as the delimiter, the other can be used inside of the string.
05:25 So that allows you to do something like an apostrophe. That’s a valid string. Again, the beginning, the end are the double quotes. Or let’s say you had a phrase.
In that case, you could use single quotes as delimiters, and inside here, you could have its own quotation. That’s valid too. So what isn’t valid is to do something like
"He said, "What time is it?"".
06:01 And you can see it already kind of behaving differently inside of here. It thinks that the delimiters are saying this is the string, and then wondering what this syntax is after that. So this is invalid.
If you need to include a quotation mark that matches the delimiter inside the type, you can do something called escaping, and that’s done with a backslash (
So you could do something like this—let’s redo
string6 and then put a
\ in. And that allows you to include a quotation mark inside of your string, or in this case, a full quote.
That’s one way to accomplish it. If you were to print out
string6, you’ll see it print out with the quotation marks inside of it. When you’re working on a project of your own, it’s a good idea to use only single quotes or only double quotes to delimit your strings. It’s sort of a consistency thing.
07:03 It helps to make your code easier to read and understand.
The number of characters contained in a string, including spaces, is called its length. To determine the length of a string, you can use the built-in length function, which is abbreviated, just
It’s going to return the number of characters contained in a string, including spaces. So try out the built-in length function. Type
len(), and then you can put, again, a string literal right inside of it.
07:39 It returns the number of characters. It also works if you’ve assigned the string to a variable. And even a phrase that has other characters than just the alphabet, it will count the spaces and the punctuation as separate characters.
08:04 What if you have a really long string? In a previous Python Basics course, you might have learned about PEP 8. PEP 8 is a style guide for Python. It recommends that each line of Python code, meaning the whole line across that you’re typing, contain no more than seventy-nine characters, including spaces.
08:23 Otherwise, it gets a little hard to contain within the screen. So what if you are, say, typing in an entire paragraph or something longer? There’s two methods that you can use to break a string across multiple lines.
One method is to add a
\ at the end of all but the last line. So right around the seventy-ninth character, you could add that
\. And the other is to use the technique I mentioned before, which is to use triple quotes as delimiters—either the double quotes or the single quotes, three at the beginning and three at the end. Let me have you try that out in the interactive window.
09:02 Again, there are two ways to work with multiline strings. Let’s take this paragraph from a book.
So putting a
\ at the end of each of the lines allows you to have a string literal that’s multiline. Oh, sorry for the typo at the beginning of it.
09:24 When you print that multiline string that’s broken up by backslashes, the output is displayed on a single line. So let’s say it wasn’t quite as big as that, but you had … and you print out the long string.
09:39 You’ll see it connect the whole thing together. The other way that you can create a very long string, such as a paragraph, is to put it inside of triple quotes.
09:56 And that will work also.
10:01 I print out that paragraph. You can see it as the whole thing again.
It will preserve the whitespace also. Let’s say you created a long string inside of that
print() statement. In this case, I’ve added a few types of whitespace …
10:23 and you can see that it preserves that inside there, the tabs and spaces that were added. You’ll learn also about how the triple quotations can be used for something called a docstring later in your Python Basics journey.
10:40 Okay. Here’s a couple exercises for you to practice what you’ve learned. Try printing a string that uses double quotation marks inside the string. Then print a string that uses an apostrophe inside of the string.
10:53 Try printing a string that spans multiple lines with whitespace preserved, and then print out a string that is coded on multiple lines, but gets printed on a single line.
11:06 Next up, you’ll learn about concatenation, indexing, and slicing of strings.
Become a Member to join the conversation.