Single quotes, double quotes and multiple quotes in python

Table of contents

single quotes in python

double quotes in python

Multiple quotes in python

When are the three used and what is the difference

Summarize


single quotes in python

In Python, single quotes (`'`) can be used to denote strings.

A simple string can be created using single quotes, for example:


name = 'John'
 

Characters within single quotes are treated as the contents of the string. In most cases, using single or double quotes to define a string is equivalent. As long as you are consistent, you can choose to use single quotes or double quotes depending on your needs.


name1 = 'John'
name2 = "John"

 

double quotes in python

In Python, double quotes (`"`) can also be used to denote strings.

Similar to single quotes, it is very common to use double quotes to define strings. A simple string can be created using double quotes, for example:


name = "John"
 

Characters within double quotes are treated as the contents of a string and can contain letters, numbers, spaces, punctuation marks, etc. In most cases, using single or double quotes to define a string is equivalent, as long as consistency is maintained.


name1 = 'John'
name2 = "John"
 

When a string needs to contain quotes, double quotes can be used to enclose the string to avoid conflicts with the quotes surrounding the string.


message = 'He said, "Hello!"'
 

When using double quotes to enclose a string, if the string itself contains double quotes, you need to use the escape character `\"` to represent the double quotes.


message = "She said, \"Hi!\""
 

It should be noted that single quotes and double quotes are used the same in Python, as long as they are consistent. Choosing to use single or double quotes is mostly a matter of personal and team preference.

 

Multiple quotes in python

In Python, three consecutive single quotes `'''` or three consecutive double quotes `"""` can be used as a multi-line string marker. This method is called a multi-line string literal or Documentation string (docstring).

Multiple quotes are a convenient way to define strings containing multiple lines of text. For example:


text = '''
This is a
multi-line string.
'''
 

Or use double quotes:


text = """
This is a
multi-line string.
"""
 

A string marked with multiple quotes can contain newlines, preserving the formatting of the original text. This is useful when defining long strings, writing comments, or writing documentation.

In addition, a multi-quoted string can also be used as a function or class documentation string to provide a description of the function or class. For example:


def my_function():
    """
    This is a sample function.
    It does some operations and returns the result.
    """
    # function body
    pass
 

It's worth noting that although a multi-quoted string can span multiple lines, each line inside a multiline string retains its original indentation, including spaces and tabs. To maintain consistent indentation in multiline strings, use string methods such as `textwrap.dedent()` for indent correction.

 

When are the three used and what is the difference

In Python, single-quoted, double-quoted, and multi-quoted strings (three consecutive single or double quotes) each have different usage scenarios and characteristics.

1. Single-quoted strings ('string') and double-quoted strings ("string"):
   - Their main purpose is to represent simple strings, which can be single-line or multi-line.
   - Single quotes and double quotes are equivalent in most cases, you can choose one according to your personal habits.
   - The choice between single and double quotes is primarily a matter of convenience when dealing with quoted strings. For example, if the string itself contains double quotes, it can be defined with single quotes to avoid the use of escape characters.
   - Both single-quoted and double-quoted strings can be used for docstrings, but in most cases, it is more common for docstrings to use multi-quoted strings.

2. Multi-quoted strings ('''string''' or """string"""):
   - Multi-quoted strings are often used to define multi-line strings to preserve the formatting of the original text.
   - Multi-quoted strings can contain newlines, keeping indentation and spaces on each line, suitable for long strings, comments and documentation.
   - The advantage of a multi-quoted string is that it avoids the frequent use of escape characters and provides better readability and maintainability.
   - A multi-quoted string is often used as a function or class docstring to provide a description of the function or class.

Summarize


- Single-quoted and double-quoted strings are suitable for simple strings, which can be single-line or multi-line. The choice of using single quotes or double quotes depends on personal habits and whether the string contains quotes.
- Multi-quoted strings are mainly used in scenarios such as multi-line strings and document strings that need to retain the original format and better readability.

For example:

name = 'John'  # 单引号字符串
name2 = "John"  # 双引号字符串

text = '''
这是一个
多行字符串。
'''
print(text)

def my_function():
    """
    这是一个示例函数。
    它做一些操作并返回结果。
    """
    pass

Guess you like

Origin blog.csdn.net/weixin_43856625/article/details/132015087