Triple Quoted Strings
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: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: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.
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.
Become a Member to join the conversation.