SoFunction
Updated on 2024-11-12

Python beginner's introduction to the single quotes, double quotes and triple quotes of the differences and application examples

If you are new to Python, then string handling will be one of the first basic skills you will need to master. In Python, a string is text surrounded by single, double, or triple quotes. Today, we're going to dive into the differences and applications of single quotes, double quotes, and triple quotes in Python to make you more comfortable on your programming journey! 🚀

First, single quotes and double quotes: in fact, there is no essential difference

In Python, single quotes (') and double quotes (") are equivalent when defining strings. Their main differences are in usage conventions and code readability. You can choose to use either single or double quotes to define a string, and the Python interpreter will treat them as the same string.

Example:

# Use single quotes to define strings
single_quoted_string = 'Hello, World!'
print(single_quoted_string)  # Output: Hello, World!

# Use double quotes to define strings
double_quoted_string = "Hello, World!"
print(double_quoted_string)  # Output: Hello, World!

In actual programming, you can choose whether to use single or double quotes depending on your preference and the readability of your code. A common practice isUse single quotes to define a string, and double quotes when quotes are needed inside a string

Second, three quotes: multi-line strings and document strings

Triple quotes (''' or """) have two main uses in Python: defining multi-line strings and defining document strings (docstrings).

  • multiline string

Triple quotes can be used when you need to define a string containing multiple lines of text. This is useful in scenarios such as writing multi-line comments, embedding code samples, or defining multi-line SQL queries.

Example:

# Use triple quotes to define multi-line strings
multi_line_string = '''This is a multi-line string.
It spans multiple lines and can contain special characters like quotes ("") without the need for escaping.
'''
print(multi_line_string)
  • documentation string

Documentation strings (docstrings) are multi-line comments used in Python to explain the purpose of a function, class, module, or method. They are surrounded by triple quotes and are located at the beginning of a function. Docstrings are available through the built-in functionhelp()or object of the__doc__attribute to access.

Example:

def add_numbers(a, b):
    """
    This function adds two numbers together.
    
    :param a: First number
    :param b: Second number
    :return: Sum of the two numbers
    """
    return a + b

# Documentation strings for access functions
print(help(add_numbers))

# Access the __doc__ attribute of a function
print(add_numbers.__doc__)

III. String splicing

In Python, you can use the plus sign (+) to splice strings. When a string defined using single, double, or triple quotes needs to be spliced, Python automatically handles the use of quotes to ensure that the resulting string is syntactically correct.

Example:

# Splicing single-quote strings
first_name = 'John'
last_name = 'Doe'
full_name = first_name + ' ' + last_name
print(full_name)  # Output: John Doe

print("*"*50)

# Splicing double-quoted strings
greeting = "Hello"
message = greeting + ", " + full_name + "!"
print(message)  # Output: Hello, John Doe!

print("*"*50)

# Splice two triple-quoted strings
first_paragraph = '''This is the first paragraph of text.
It contains multiple lines and is defined using triple quotes.'''

second_paragraph = '''This is the second paragraph.
It is also multi-line and defined using triple quotes.'''

# Splice the two strings using the plus operator
combined_text = first_paragraph + '\n\n' + second_paragraph

# Print the spliced string
print(combined_text)

Output:

John Doe
**************************************************
Hello, John Doe!
**************************************************
This is the first paragraph.
It contains multiple lines and is defined using triple quotes.

This is the second paragraph.
It is also multi-line and defined using triple quotes.

Process terminated, exit code 0

IV. Summary

In Python, single quotes, double quotes, and triple quotes are commonly used to define strings. Single quotes and double quotes have no essential difference in function; the main difference is in usage and code readability. Triple quotes are mainly used to define multi-line strings and document strings. Mastering the use of these quotation marks will help you handle strings more flexibly and improve the efficiency and quality of Python programming.

To this point, this article on the Python Beginner's single quote, double quote and triple quote differences and application examples of the article is introduced to this, more relevant Python single quote, double quote and triple quote application content, please search for my previous posts or continue to browse the following articles hope that you will support me in the future more!