Python strings

Codestars by Rob Percival
A free video tutorial from Codestars by Rob Percival
Teaching the Next Generation of Coders
4.5 instructor rating • 57 courses • 1,594,129 students

Lecture description

In this lecture we talk about strings in Python. Strings are just collections of characters, numbers, and symbols. We also talk about how to use existing variable in strings to make them more dynamic.

Learn more from the full course

The Complete Python Course | Learn Python by Doing

Go from Beginner to Expert in Python by building projects. The best investment for your Python journey!

34:55:31 of on-demand video • Updated July 2020

  • From foundations to expert, learn about every major Python topic, working with Python 3.
  • Write professional-grade Pythonic code with all the best practices and avoiding common pitfalls.
  • Master Object-Oriented Programming and structure your Python programs like a professional.
  • Swiftly understand complex topics like decorators, algorithms, and asynchronous programming in Python.
  • Automate extracting data from websites using web scraping libraries like BeautifulSoup and Selenium.
  • Learn how to write desktop applications with Python and Tkinter.
  • Interact with REST APIs using Python and build a currency converter!
  • Develop a life-long love for programming with Python!
English -: Hi guys and welcome back. Very often in our programmes, we'll be dealing with numbers. But almost as often, if not more, we will want to deal with text. The user will be giving us some text that we want to interact with. We will be doing some processing on it, we will be wanting to print some text out to the screen, and that is where the next datatype comes in. In Python, we've got another type called strings that are used to contain text like characters, symbols, numbers, or anything else we want. Here's our first string that I'm gonna call my string and it contains the value, hello world. So a couple of things about this string is that it is surrounded by double quotation marks. You can also use single quotations mark if you like, but it's important that the start and the end quotation mark match. So they have to be the same, you can't mix and match them. So this here would be invalid because the start quote and the end quote are different. I like using double quotation marks, but that's just me. You can use whichever one you like. This string here contains the characters H, E, L, L, and so on, including the comma and the space and the exclamation mark. When Python looks at this string, it knows nothing about it. It doesn't care what its contents are. It doesn't have any meaning like the number 30 does because you can do maths on it, for example. A string doesn't have any intrinsic meaning for Python. All it's used for is to carry these characters. For example, you can use it to print some information out to the user, like hello world. If you wanted to print that out, you can store it inside a string and then print it. As an aside, remember you can always use the value itself instead of a variable inside the print function here if you prefer. Sometimes you'll want to use one quotation mark or the other depending on the contents of the string. So if you've got a string with quotes, then you'll want to use the double quotes, such as, hello, it's me. Here, you definitely want to use the double quotation marks on the outside because you've got a single quotation mark on the inside. If you were to use single quotation marks throughout, then this part here is not included in the string because the string ends here. If you run this, you'll get an error. Similarly, you may want to use the single quotation marks on the outside and the double quotation marks on the inside in some occasions. For example, if you have something like this. Here we've got a string and inside the string we've got a bunch of characters. Once Python realises that you're dealing with a string you can put whatever you want inside it, and in this case we are putting the quotation marks inside it. He said, and then open quotes, you are amazing, yesterday. This is totally fine because these quotation marks here don't signal another string to Python, they are just a character inside the string. Finally, if you really wanted to always use the same quotation marks, you can do. So I'm gonna turn this into a double quotation mark string. And now you can see that these things here are not part of the string, they are coloured differently. However, if you put a backslash in front of these quotes, Python will no longer treat them as characters used to signal a string. Now they are part of the string itself. I highly discourage you from doing this, but you may see it every now and then in code so I wanted to tell you what it means. When you do this, this is called escaping and it's quite a common thing to do in some occasions. But when you're dealing with strings like this, I would always recommend that you invert the quotes. We also have multiline strings for when we wanna print something out that's much longer. So we will have something like a multiline variable and here we are gonna use three quotation marks. And then we can print whatever we want inside it. If we print this out and we run it, you'll see that we get multiple lines printed out. So we've got the hello world, then we've got an empty line, then we've got my name is Jose, welcome to my programme, and then we've got another empty line as well. Multiline strings are very useful when you have much longer pieces of text that you want to print out, and also they can be useful at times as comments. Earlier on in the course, we saw that a comment is signalled with a hash symbol. So you can write something here to tell yourself what this file is about. But similarly, sometimes you have a longer comment. You wanna write multiple lines of notes. And for that, it is quite common to use one of these multiline strings. So why is this? Why can you do this? Well, what this does in Python is it creates the string, and then, that's it. You don't assign it to a variable, you don't use it anywhere. This is totally fine to do in Python and we can use a multiline string like this that doesn't have any usefulness inside the programme to leave a comment for ourselves for later on. So often you'll be seeing multiline strings like this in my files with explanations about what things are. Just one more thing, you can add strings together. So if you've got a name, such as Jose, and a greeting, you can add it to name and then we will end up with hello comma space Jose. So when you add two strings together, they are joined and make up one final string. So I will print out this greeting here and run the file, and you'll see that we get hello, Jose. If you do age is 34 and then you try to add this to a string like here, you are plus 34, you will get an error because in Python, you cannot add integers and strings together. You must convert this to a string first before you can add them together. Fortunately, converting to a string is very straightforward. You can either add the quotation marks around it or you can do str and pass in age. What this does is it takes in the number 34, you give it to this str function, which has these brackets around it, to accept a value. So you're giving the age value to str and what you get back is a string with this content inside it. So then you can still run this because age as str is a string that you've converted. We will be learning more about converting data from one type to another as we move through the course and it's a very common thing to do. But for now, that's it for this video. Thank you for joining me and I'll see you in the next one.