Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Strings

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.

Avatar image for Paul D Paradis

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.

a = 'I wasn''t in school today'

Why is the output basically the concatenation of the entire thing?

print(a) -------> I wasnt in school today

Avatar image for Dan Bader

Dan Bader RP Team on April 23, 2021

@abp: Ah, great question! This feature in Python is called string literal concatenation:

Multiple adjacent string or bytes literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation. Thus, "hello" 'world' is equivalent to "helloworld". (Source)

So, whenever you put two (or more) string literals next to each other, Python will merge them into a single string:

>>> s = "my" "string" "is" "here"
>>> s
'mystringishere'

And it even works with comments placed in-between:

>>> print("hello " # this is a comment
...       "world" # another comment
...       "!")
hello world!

String literal concatenation makes splitting long strings across multiple lines super easy:

LOREM_IPSUM = (
    "Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
    "Donec odio. Quisque volutpat mattis eros. "
    "Nullam malesuada erat ut turpis. "
    "Suspendisse urna nibh, viverra non, semper suscipit, "
    "posuere a, pede. Donec nec justo eget felis facilisis "
    "fermentum. Aliquam porttitor mauris sit amet orci."
)
print(LOREM_IPSUM)

Output:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci.

Compared with a triple-quoted string:

LOREM_IPSUM = """
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    Donec odio. Quisque volutpat mattis eros. 
    Nullam malesuada erat ut turpis. 
    Suspendisse urna nibh, viverra non, semper suscipit, 
    posuere a, pede. Donec nec justo eget felis facilisis 
    fermentum. Aliquam porttitor mauris sit amet orci.
"""
print(LOREM_IPSUM)

Output:

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    Donec odio. Quisque volutpat mattis eros.
    Nullam malesuada erat ut turpis.
    Suspendisse urna nibh, viverra non, semper suscipit,
    posuere a, pede. Donec nec justo eget felis facilisis
    fermentum. Aliquam porttitor mauris sit amet orci.

Note how the triple-quoted string above includes blank lines, linefeeds, and the leading spaces used for indentation. The single-quoted version does not.

Depending on what you’re trying to do, this can be a really useful feature that you can use to your advantage in your Python programs.

Become a Member to join the conversation.