Python Fundamentals 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.
[00:00:34] 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.
[00:01:02] 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.
[00:01:24] 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.
[00:01:57] 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.
[00:02:27] 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.
[00:02:50] 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.
[00:03:21] 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.
[00:03:49] 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.
[00:04:12] 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.
[00:04:49] 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.
[00:05:17] 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?
[00:05:48] 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'.
[00:06:17] 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?
[00:06:33] 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.