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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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 your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

Triple Quoted Strings

00:00 Strings: Triple-Quoted Strings.

00:04 Triple-quoted strings are most easily demonstrated within a program itself, so that’s what you’re going to see here. As you can see onscreen, the triple-quoted string has three quote characters (""") at each end and they can be single or double as long as they match each other.

00:22 Printing this out, it just looks like a normal string,

00:28 but triple-quoted strings obviously have more to offer us just than extra typing. It’s possible to put any kind of quote inside. As you can see here, the same quotes are being used inside the string as that enclose it.

00:42 As you can see printed out here, the double quotes work perfectly normally, but it’s also possible to use single quotes inside this triple-quoted string as well. So you can see, they offer an alternative to using escape characters, which you may find simpler to understand. However, their main use isn’t to avoid the use of escape characters, but it’s to allow strings which cover multiple lines, as you’re going to see next onscreen.

01:20 And once that gets printed out, you can see it works exactly as you’d expect. While this is a convenience, it probably isn’t the most common use for triple-quoted strings.

01:31 Their most common use is providing docstrings for functions. Onscreen, first, a function is being created, as you can see. It’s a simple function that just takes a value and prints it out.

01:50 Now, while this is clearly a simple function and doesn’t need much explanation, most functions you create will be more complicated than this, and when you come back to them it may take you a while to understand what they are or what they do, particularly when they’re named similarly.

02:04 A good way to deal with this is to insert a docstring straight after the definition of the function. This will take the form of a triple-quoted string which provides a description of what the function does and what the parameters are that you feed to it. While this may make sense while you’re reading through the code, it’s also useful while you’re writing code. With most editors, when you use a function it will pop up a hint which is made from the docstring that tells you about the function, as you can see here. Takes a value and prints it to the screen has been replicated.

02:38 This may not be that much use in a simple program, but where you have a complicated one with multiple imports from multiple files, this can be really useful in telling you what the functions do. And notice that this is where the information that you see when using standard functions such as print() also comes from.

02:56 Any imported modules you’ve used in your programs will also have docstrings, which will help you understand how each function works.

Cristian Palau on April 8, 2020

Great explanation Darren, if we need to add \n or ” in a strings what is the best option, escape sequences or directly use triple quoted strings? Maybe there is a convention or a good python practice for this. Thanks!

ana on Dec. 24, 2020

Sir if i write the same code in cmd i am not able to get the result instead i am getting the result as

def my_function(value):
... print(f"{value} is a nice value")
  File "<stdin>", line 2
    print(f"{value} is a nice value")

I am getting this as the result could you please explain why this is happening?

Darren Jones RP Team on Dec. 29, 2020

Ana - I think you need to indent your print line - from the printout you have presented, it looks as if you need to hit TAB before typing print(f"{value} is a nice value")

Marat Sabirov on July 19, 2021

Darren, thanks for excellent explanations! What Python editor you have used? I saw nice pop-ups with function paramters! Can I achieve the same in Jupyter notebooks, for example?

Bartosz Zaczyński RP Team on July 20, 2021

@Marat Sabirov It looks like Visual Studio Code to me, but I may be mistaken.

Marat Sabirov on July 20, 2021

@Bartosz Zaczyński, thanks! I’ll check it.

keddy on Feb. 3, 2023

I like the idea of documenting (as you code) with docstrings. However, I do not yet see how it works in the Thonny ide I am using. Maybe you could suggest which ide would best demonstrate this?

Martin Breuss RP Team on Feb. 6, 2023

@keddy Darren uses Visual Studio Code in the course, like @Bartosz mentioned further up. It’s a solid choice as a code editor.

You could also take a look at finding the perfect Python code editor if you want to browse some more to select a different one that works best for you.

Become a Member to join the conversation.