Hướng dẫn python triple quote

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • 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.
     

    Syntax: “”” string””” or ”’ string”’

    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 Strings

    Python3

    Similarly, single triple quotes can also be used for the same purpose as shown below: 

    Python3

    Note : We can also uses # in multiples lines, but triple quotes look much better.
     

    Triple Quotes for String creation

    Another 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

    str1 =

    str2 =

    str3 =

    print(type(str1))

    print(type(str2))

    print(type(str3))

    print(str1 + str2 + str3)

    Output:

    
    
    
    I am a Geek

    Example 2: 
    Multi-line strings using triple quotes. End of lines are included by default.  

    Python3

    my_str =

    print(type(my_str))

    print(my_str)

    Output:

    
    I
    am
    a
    Geek !

    Example 3: 
    If we wish to ignore end of lines, we need to use ” .

    Python3

    my_str =

    print(type(my_str))

    print(my_str)

    Output:

    
    I am a Geek !


    Hướng dẫn python triple quote

    Photo by Yann Allegre on Unsplash

    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 Quotes

    Basic Usage

    The 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'
    >>> quotes_double = "a_string"
    >>> quotes_single == quotes_double
    True

    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'
    File "", line 1
    "mixed quotes'
    ^
    SyntaxError: EOL while scanning string literal
    >>> 'mixed quotes"
    File "", line 1
    'mixed quotes"
    ^
    SyntaxError: EOL while scanning string literal

    Escaping Behaviors

    Like 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.'
    File "", line 1
    'It's a bad example.'
    ^
    SyntaxError: invalid syntax

    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.'
    "It's a good example."
    >>> "It's a good example."
    "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!"'
    '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."')
    File "", line 1
    print('She said, "Thank you! It's mine."')
    ^
    SyntaxError: invalid syntax
    >>> print('She said, "Thank you! It\'s mine."')
    She said, "Thank you! It's mine."

    Triple Quotes

    Enclosing Strings Containing Single and Double Quotes

    As 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."''')
    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."""")
    File "", line 1
    print("""She said, "Thank you! It's mine."""")
    ^
    SyntaxError: EOL while scanning string literal

    Multi-line Strings

    Another 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
    ... World
    ... !""")
    Hello
    World
    !

    Although we can achieve the same effect by using the \n symbols to create multi-line strings as below, using the \n symbols makes the string harder to read. By contrast, using triple quotes can write the string in the way how it exactly looks like, and thus it has better readability.

    >>> print('Hello\nWorld\n!')
    Hello
    World
    !

    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):
    ... '''
    ... a is a string # other additional description
    ... b is a list of integers # other additional description
    ... '''
    ... pass
    ...
    >>> print(multiple_line_comment.__doc__)

    a is a string # other additional description
    b is a list of integers # other additional description

    We can clearly tell what are the comments for the function.

    Conclusions

    This 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

    • Enclose strings. Either single or double quotes are fine.
    • Use single quotes as enclosing quotes to eliminate the need of escaping double quotes in a string, and vice versa.

    Triple Quotes

    • Enclose strings containing both single and double quotes such that no escaping is needed.
    • Enclose multi-line strings.

    Thanks for reading this article, and happy coding in Python.