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