How are strings enclosed in Python?

0139

In Python, strings can be enclosed in single quotes ('), double quotes ("), triple single quotes ('''), or triple double quotes ("""). Here are examples of each:

  1. Single quotes:

    single_quoted_string = 'Hello, World!'
  2. Double quotes:

    double_quoted_string = "Hello, World!"
  3. Triple single quotes (often used for multi-line strings):

    triple_single_quoted_string = '''This is a
    multi-line string.'''
  4. Triple double quotes (also used for multi-line strings):

    triple_double_quoted_string = """This is another
    multi-line string."""

You can use either single or double quotes interchangeably, but if your string contains a quote character, you may want to use the other type to avoid escaping.

0 Comments

no data
Be the first to share your comment!