Check out a free preview of the full Python Fundamentals course

The "Strings" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces best practices for strings, string concatenation, long strings, and f strings


Transcript from the "Strings" Lesson

>> Nina Zakharenko: Strings in Python. They can be enclosed either with single quotes, so like this. Or in double quotes, like that. We'll see that the representation of them is the same, it doesn't matter how you declare them, you'll end up with the same result. In general it's best practice to use double quotes because if you need to put a single quote within your string.

If you were to do it,
>> Nina Zakharenko: Like this, you would get a failure, right? Because you closed the string before this last closing string. It's a syntax error. So if you're using enclosing single quotes and you wanna put another single quote in between, you have to escape it with a backslash.

And that's just kind of yucky, right? If we use double quotes from the get-go, then we can have our single quotes in there, no problem. It's a lot more readable. You might see single quotes within some of these examples, I'm sorry I migrated over a few years ago.

So you can also concatenate two strings together with the plus symbol. We won't be using too many long strings on this first day. But if you need declare a long string, you can do that with triple quotes. It's also how you'll see long form comments in the code.

So if you say long_string = you put triple quotes and you press Enter, you'll see a different prompt here in the REPL. It'll be three dots, and that means that the REPL is waiting for more input. So here we can type, oops. Here we can type more stuff and once we're done with this long string, we can end it with another three quotes.

I'm gonna press Enter. And if we look at that, we'll see a long string. Might be wondering what these \n characters are. We'll talk about them more later, but they represent a new line. So a new line here in the beginning, a new line after 1234, and a new line after abc.

There are several types of string formatting, but one of the biggest advantages of Python 3 is something called f-strings. It's the new fancy way of string formatting.
>> Nina Zakharenko: Let's say we made a new variable called name, we assigned it to our names.
>> Nina Zakharenko: To declare an f-string, the f-string starts with the lower case letter f followed by the opening double quotes, just like usual.

Now I can type stuff in my string. And if I want to reference a variable, I use curly braces.
>> Nina Zakharenko: I'm gonna reference the variable name and then put my closing quote here.
>> Nina Zakharenko: When I press Enter it's gonna do that interpolation for me and say, Hello, Nina.

Is everyone able to get hello and their name printed out with an f-string? Awesome. So there are a few other types of string formatting that you might see in Python code, especially older Python code. One of those is called percent formatting. You might see the .format method, but throughout the course of today and tomorrow, generally I'm gonna use f-strings.

They are really easy, they're pretty easy to get the syntax right. And if you're learning the the best way first, then you'll end up at an advantage. Going back to the course website, I wanna talk about a few common mistakes that you might see here. So you can open this up for reference, but I'm gonna go through them in the REPL, excuse me.

Okay, someone said CTRL + L, I believe, to clear my REPL. If that shuts down my VS Code, you're getting in trouble. [LAUGH] Thank you. Thank you random chat person, much appreciated. Okay, so a few common mistakes. If you mismatch string quotes, so if I do that and I do that and I press Enter, you're gonna get this exception.

Syntax error, end of line. Or EOL, eol stands for end of line while scanning string literal. The error's kind of pointing to exactly where you made the mistake, so just take a quick look there.
>> Nina Zakharenko: Another error you might commonly see, if you try to concatenate an integer in a string, because we can concatenate two strings, right?

But if we try to concatenate a string and a number, we're gonna get an exception. It's gonna be a TypeError. And it can say, can only concatenate str not int to star. And you might get a slightly different error if you do it the other way round. Unsupported operand types for +: 'int' and 'str'.

So I know these tracebacks look kind of scary, but if you just read them from the bottom up, we can see here that we try to add a number together in a string and it doesn't work, just be mindful of when your stuff is all in variables, right?

So if I have a = 5 and name = Nina, and I try to add these together, it's a litle bit less immediately obvious of what's going on, right? So just keep an eye out for that error.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now