Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.


Give Feedback

00:00 Strings.

00:05 So, what is a string? A string is a sequence of zero or more characters. Strings can be enclosed in double quote (") or single quote (') characters, and if you need to include either of those in a string, it’s possible to do that as strings can also take the format of raw or triple-quoted strings, both of which you’re going to see later in this section.

00:31 Defining a string is straightforward, as you can see here, defining the string "hello world" enclosed in double quotes, but it can also be enclosed in single quotes.

00:44 And as you can see, both have the same representation when printed by Python. Choosing which to enclose the string in can be important because sometimes you’ll want to have one or the other inside a string. So here you can see a string is being defined using double quotes, and it has a single quote inside it, which Python is untroubled by.

01:10 It’s happy to print that out. But if that was done using single quotes throughout, bpython is already giving us a colored hint that this isn’t right, and we have a SyntaxError because we have the beginning of the string which is defined up to 'I wasn', and then the rest of the line doesn’t make sense to Python.

01:33 So that is not the way that you do that. Now clearly, you can do it the other way around. You could have a single-quoted string which has double quotes inside, and that will work perfectly well.

01:45 But trying to do that using double quotes throughout won’t work in the same way that we’ve already seen, and you can see that bpython is color coding our problem to highlight it to us.

01:58 Python has got confused about this. We know that that should be the whole string, all the way there, but that’s not the way that Python sees this. Sometimes there’ll be examples when you need to use single and double quotes inside a string, and you’re going to see how to do that a little bit later.

02:18 As you would expect from the other videos you’ve already seen, if we use type() on one of the strings we’ve defined, we can see it’s of the str string class, and this can be used to convert other data types into strings.

02:33 So if we want to turn the number 51 into a string, we can just define it using g = str(51), and now g is the string '51', rather than the int.

Paul D Paradis on Aug. 5, 2020

This is gold! I have been teaching myself Python for a while now, and this is the best presentation of fundamental data types I have ever come across. I just joined real Python and decided to go through the basics as a refresher and to get comfortable with the site, and I am thoroughly impressed.

Become a Member to join the conversation.