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: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.
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.
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: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: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.
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.
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.
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: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.
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.
Become a Member to join the conversation.