Python Strings Overview
In this video, you’ll get to review the string data type. You will see what strings are and how to work with them in Python. Strings and lists are closely related data types. You’ll learn what they have in common as well as what makes them different. By the end of the lesson you’ll have a good foundation for the next lessons that cover the basic string methods like splitting, concatenating, and joining.
00:00 Hello and welcome! In this series of videos, we’ll discuss what it means to split, concatenate, and join strings in Python. You may already be familiar with these operations, but we’ll look a little more closely at how they work. First, let’s take the opportunity here to review how Python defines the string data type.
00:20 You likely already know that a string is the way we work with text in our program. We can specify a string by placing text between a pair of similar quotation marks.
00:30
We’ll start with a simple string variable assignment, and we’ll name our variable sentence
. Great! We have a string! So, what can we do with it?
00:38
Let’s check out what the type()
function tells us about our new variable.
00:44
As you can see, Python considers the sentence
variable to be an instance of the class str
, or the string class. Since sentence
is a string object, we have some useful methods at our disposal.
00:56
If you’re unfamiliar with the term method, it simply means an object-specific function. A method can be accessed and used from the object instance by using the dot (.
) operator. Methods are like an object’s verbs or action words. They help define what an object does.
01:16 Both string literals and string variables are string instances. And once they appear in our program, we can access their methods. How do we know what those methods are?
01:27 And when we find out, how do we know how to use them? To help illustrate this next part, I’m going to switch to the console.
01:36
Starting with my same sentence
variable, I’m going to pass it as an argument to the dir()
function, which will tell me the attributes available on my string instance.
01:44
dir()
returns a list of those attribute names as strings. The dir()
function works whether that string instance is a string variable, a string literal, or even the class name itself.
01:59
Another useful tool that we can run in the console is the help()
function, except that it doesn’t work if we pass it in a string instance. We have to pass it in the string class (str
).
02:10
What’s returned from the help()
function is much more verbose, but also more helpful if you want to see documentation on how various string methods operate. For a quick demonstration on how to use string methods, let’s start with the .upper()
method.
02:23
The job of .upper()
is to create an uppercase string based on a given string. It is not recommended or usual to invoke the methods directly from the class as we’ve done here. But if we did, we’d have to pass our string to the method as an argument.
02:39
Still, .upper()
would complete its job, creating a new uppercase string. Let’s see this in the console. As you can see, we got our uppercase string and it’s a new string.
02:52 It’s more common to access the string methods, however, off of a string literal or string variable.
03:00 To fully understand strings and their methods, it helps to compare strings and lists. That’s because strings and lists are like cousins. They are both sequence data types. While a list is a sequence of elements separated by commas, a string is a sequence of single characters, so we can sometimes use lists and strings in similar ways.
03:21
For example, we can pass both lists and strings into standalone functions that understand sequence data types, such as len()
.
03:33
We can use an index to retrieve a value at a position in the sequence. And we can use slicing to create a new list or string from a portion of the sequence. Because they are both sequences, we can also loop through them gracefully with a for
loop.
03:50 So, we’ve observed that strings and lists do indeed have a lot in common, but what’s ultimately important and key to how string methods operate is to understand how strings are different fundamentally from lists.
04:03 And that fundamental difference is that strings are immutable. Put simply, that means strings can’t be changed. Therefore, none of the string methods has the ability to modify the original string. Let’s see this at the console.
04:18
Here we are back at the console with our original list and string variables. If I want to change, let’s say, my third element in scores
from a 21
to a 20
, I can do that. I’ll simply reassign the value at that position in the list.
04:34
I don’t get a new list when I do this, so I have to look inside to see that it’s modified. I can’t do something similar with sentence
, because strings are immutable.
04:42
And you’ll see that when I try to, I get an error message. Let’s look again at how lists are mutable, but strings are not. list
has an .append()
method, so I can use that to put a 17
at the end of my scores
list.
04:56
I don’t get a new list, but the original has been forever changed. Can’t do the same thing with sentence
. There’s nothing like an .append()
method.
05:05
There’s no way I can tack a character on to the end of this string. This string is immutable. So if I try it, I’m going to get an error, mostly because there is no .append()
method.
05:18
Let’s look again at .upper()
. You’ll see that the behavior is different between list.append()
and str.upper()
. When I hit Return here, I get a new string.
05:28
So, the .upper()
method did not modify the original. It took the original and created a new string. Therefore, if we look inside our sentence
variable, it shouldn’t surprise us that nothing has changed. None of the string methods, including .upper()
, is allowed to do so. String methods like .upper()
return new values, so your program would typically handle that return value.
05:51 Like, for example, with a variable assignment.
05:56
I hope taking the time to review the string data type has given you a good foundation for our next video, where we continue our exploration of string methods with .split()
. See you there!
Become a Member to join the conversation.