Hướng dẫn python triple quote
View Discussion Show
Improve Article Save Article View Discussion Improve Article Save Article Spanning strings over multiple lines can be done using python’s triple quotes. It can also be used for long comments in code. Special characters
like TABs, verbatim or NEWLINEs can also be used within the triple quotes. As the name suggests its syntax consists of three consecutive single or double-quotes.
Note: Triple quotes, according to official Python documentation are docstrings, or multi-line docstrings and are not considered comments. Anything inside triple quotes is read by the interpreter.
When the interpreter encounters the hash symbol, it ignores everything after that. That is what a comment is defined to be. Triple Quotes for Multi-Line StringsPython3Similarly, single triple quotes can also be used for the same purpose as shown below: Python3Note : We can also uses # in multiples lines, but triple quotes look much better. Triple Quotes for String creationAnother use case of triple quotes is to create strings in Python. Adding the required characters within triple quotes can convert those characters into python strings. The below codes shows the use of triple quotes for creating strings: Example 1: Python3
Output:
Example 2: Python3
Output:
Example
3: Python3
Output:
It’s always good to have alternatives — single and double quotes are essentially used interchangeably in Python.All of us Python programmers know that there is usage of single and double quotes related to the declaration of the strings in Python. However, not all of us know that there is certain usage involving triple quotes too. This brief article reviews the usage of single, double, and triple quotes in Python. Single and Double QuotesBasic UsageThe most common use of single and double quotes is to represent strings by enclosing a series of characters. As shown in the code below, we create these two strings using single and double quotes, respectively. >>> quotes_single = 'a_string' As you notice, the strings created by using single and double quotes are the same. In other words, we can use single and double quotes interchangeably when we declare a string. However, it should be noted that we don’t want to mix them as it’s a syntactical error. >>> "mixed quotes' Escaping BehaviorsLike other programming languages, when a string contains special characters like quotes, we need to escape them. An example of failing to escape is shown below. >>> 'It's a bad example.' How can we fix this error? One is to escape the single quote by placing a backslash before it. The other is to use double quotes instead of single quotes as the enclosing quotes. Both ways are shown below. >>> 'It\'s a good example.' Similarly, if the string contains double quotes, we can use single quotes to represent the string such that we don’t have to escape the double quotes. An example is given below. >>> 'She said, "Thank you!"' However, if there are both single and double quotes in the string, it’s a syntactical error if you don’t escape the quotes that are the same as the enclosing ones used by the entire string. >>> print('She said, "Thank you! It's mine."') Triple QuotesEnclosing Strings Containing Single and Double QuotesAs mentioned at the end of the above section, we need to escape single or double quotes depending on what enclosing quotes the string uses. Actually, we can use triple quotes (i.e., triplet of single quotes or triplet double quotes) to represent the strings containing both single and double quotes to eliminate the need of escaping any. >>> print('''She said, "Thank you! It's mine."''') It should be noted that when a string starts or ends with a single or double quote and we want to use the triple quotes for the string, we need to use the ones that differ from the starting or ending one. For example, for the string in the above code snippet, using triple double quotes would result in a syntactical error. In this case, we want to use the triple single quotes as above. >>> print("""She said, "Thank you! It's mine."""") Multi-line StringsAnother use case of the triple quotes is to represent a multi-line string. An example is given below. You can use either triple single or double quotes in this case. >>> print("""Hello Although we can achieve the same effect by using the >>> print('Hello\nWorld\n!') In addition, a useful application of the triple-quote enclosed strings is to specify some comments in a multi-line string, for example, as part of a function definition like below. >>> def multiple_line_comment(a, b): We can clearly tell what are the comments for the function. ConclusionsThis article reviewed the common usage of single, double, and triple quotes in Python. Here’s a quick summary of these use cases. Single & Double Quotes
Triple Quotes
Thanks for reading this article, and happy coding in Python. |