Search code examples
pythonfunctionquote

"""Different""", "quote", 'types' in function?


I am trying to figure out if the different quote types make a difference functionally. I have seen people say its preference for "" or '' but what about """ """? I tested it in a simple code to see if it would work and it does. I was wondering if """ triple quotes """ have a functional purpose for defined function arguments or is it just another quote option that can be used interchangeably like "" and ''?

As I have seen many people post about "" and '' I have not seen a post about """ """ or ''' ''' being used in functions.

My question is: Does the triple quote have a unique use as an argument or is it simply interchangeable with "" and ''? The reason I think it might have a unique function is because it is a multi line quote and I was wondering if it would allow a multi line argument to be submitted. I am not sure if something like that would even be useful but it could be.

Here is an example that prints out what you would expect using all the quote types I know of.

def myFun(var1="""different""",var2="quote",var3='types'):
    return var1, var2, var3

print (myFun('All','''for''','one!'))

Result:

('All', 'for', 'one!')

EDIT:

After some more testing of the triple quote I did find some variation in how it works using return vs printing in the function.

def myFun(var1="""different""",var2="""quote""",var3='types'):
    return (var1, var2, var3)

print(myFun('This',
'''Can
Be
Multi''',
'line!'))

Result:

('This', 'Can\nBe\nMulti', 'line!')

Or:

def myFun(var1="""different""",var2="""quote""",var3='types'):
    print (var1, var2, var3)

myFun('This',
'''Can
Be
Multi''',
'line!')

Result:

This Can
Be
Multi line!

Solution

  • From the docs:

    String literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). [...other rules applying identically to all string literal types omitted...]

    In triple-quoted strings, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the string. (A “quote” is the character used to open the string, i.e. either ' or ".)

    Thus, triple-quoted string literals can span multiple lines, and can contain literal quotes without use of escape sequences, but are otherwise exactly identical to string literals expressed with other quoting types (including those using escape sequences such as \n or \' to express the same content).

    Also see the Python 3 documentation: Bytes and String Literals -- which expresses an effectively identical set of rules with slightly different verbiage.


    A more gentle introduction is also available in the language tutorial, which explicitly introduces triple-quotes as a way to permit strings to span multiple lines:

    String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line. The following example:

    print("""\
    Usage: thingy [OPTIONS]
         -h                        Display this usage message
         -H hostname               Hostname to connect to
    """)
    

    produces the following output (note that the initial newline is not included):

    Usage: thingy [OPTIONS]
         -h                        Display this usage message
         -H hostname               Hostname to connect to
    

    To be clear, though: These are different syntax, but the string literals they create are indistinguishable from each other. That is to say, given the following code:

    s1 = '''foo
    'bar'
    baz
    '''
    s2 = 'foo\n\'bar\'\nbaz\n'
    

    there's no possible way to tell s1 and s2 apart from each other by looking at their values: s1 == s2 is true, and so is repr(s1) == repr(s2). The Python interpreter is even allowed to intern them to the same value, so it may (or may not) make id(s1) == id(s2) true depending on details (such as whether the code was run at the REPL or imported as a module).