Transcript from the "Strings" Lesson
>> If we want to represent strings, there are two ways that we can do that, Python accepts both single quotes and double quotes, if you are Defining a string, make sure that whatever type of quote that you use, make sure that it matches or you're going to get an error.
[00:00:27] I prefer double quotes because if you have a sentence like this, is this going to work, when I enter this into my ripple? No, it's gonna blow up because Python thinks that this string has ended here. So if you're using single quotes and you have a single quote inside of your string, you're going to want to escape it with the backwards slash.
[00:00:56] And now Python does the right thing, or you can just use double quotes and then you don't have that problem. Because single quotes can be nested inside of double quotes without issue and without escaping, because Python knows that the match for this first double quote is this second double quotes If you have long strings You can start them with three quotes, either double or single, and then you can press enter.
[00:01:34] And then to end your very long string, you wanna use three quotes again. If we take a look at the string we'll see that it doesn't look like this when we print it out, it's separated by \n, \n is the symbol for new line, \t is the symbol for tab.
[00:01:57] If we print this out, we'll see that it looks how we expect it to but that's not how Python stores it under the hood. So printing it out is basically asking for how it might look like when it's rendered. Now print is something that you'll be using a lot, you can print out strings, you can print out variables.
[00:02:29] And something that I find very handy in print is that you can actually pass in multiple values to print. And if they're separated by a comma, the output of print will put a space in between them, so I can say print hi and then a, and it'll print hi 5.
[00:02:51] This is just kind of a quick and handy shortcut when you don't wanna use an F string, when you don't wanna format something, you just wanna print it out see what it looks like. I use this all the time, but for some reason it is not common syntax.
[00:03:09] You can also concatenate strings, just like you added two numbers together, you can add two strings together. And you can do that with the plus symbol, know that you don't wanna use string concatenation for very large or very large numbers of strings. I'm talking like, really, really large cuz then things start slowing down, you probably won't run into that problem for a very long time though.
[00:03:38] Now, I showed you F strings a little bit earlier, those are the modern way of formatting Python strings. So an F string just starts with the letter F, and you can put curly braces in here. And inside of those curly braces, you can put an expression, so in this case, I'm going to put the variable a because I've already defined it.
[00:04:09] You want to be mindful though, I don't have a variable b, so if I try to do that I'm going to get an error that says name b is not defined, kind of looks like we would expect it to, right? You can also pass in F strings into print and use them like you would regular strings.
[00:04:31] They are really great because there are a few old ways of formatting strings in python that you might run into that are not as nice, they are error prone. How you saw a lot of those errors like can't concatenate number and string, F strings take care of that for you under the hood.
[00:04:50] So if you're just learning Python and you don't have any attachment to the old way of formatting, reach straight for this and you will be much, much happier. Somebody asked, is there an auto decrement like plus plus and minus minus. No, I don't believe that there is, there is a plus equals and a minus equals.
[00:05:14] If you want to work with some of the methods I showed you on strings earlier, there are some examples of that on the course website that I won't dive into. If you want to, I'll just show you an example of one though, if you want to replace characters like if I have a name = Nina, if I wanted to call a method on this, I would just use the variable name and dot.
[00:05:41] And in this case I can call replace and I would replace, and let's see, and that returns Zina. You can see here though that the original string was not modified, it's because strings in Python can't be changed. So if you want to do something with this string, you're going to have to assign it to a new variable.
[00:06:11] So I could say, z_name=name.replace, oops, and see, and z, if I take a look in that variable, I've now saved the value of running this function. Are there any other quick questions before I move on to lists?
>> I read somewhere that strings are immutable in Python
>> When would you replace the N with the C?
>> So if you saw this code right here, running name.replaced returned in new string. And if I took a look at my name variable here, you will see that the value was not changed, I had to save the result of name.replace to a different variable before I was able to save it.
[00:07:10] So, the value in named did not change and we can verify that one more time. So the question was, when you're using F strings, if you want to use curly braces in your output, how would you do that? I believe that you can escape them just like you would with other special characters by putting a backslash in front of them.
[00:07:40] F string, this might need a double backslash, and if this doesn't work, I would have to look it up. Yeah, I'm not sure I would have to look that up, I don't think I've never done this before, so it's a great question.
>> What if we want to declare a variable with a data type, but we don't mention its value?
[00:08:07] Like for example, a of type string but we don't give it any value.
>> So, to have a variable that doesn't have a value you can just use the non keyword
>> As the datatype string.
>> It doesn't have a data type, so if we look at the type of a we'll see that the type is actually none type.
[00:08:31] So this symbolizes no or nothing, with Python you don't have to declare types of variables before you declare them.