Python 3: Quoting String

By Xah Lee. Date:

Strings are enclosed using single quote or double quote. example

# python 3

a = "this "
b = 'and that'
print(a, b)

You can use \n for linebreak, and \t for tab, etc.

# python 3

a = "this\nthat\n"    # use \n for line-break

b = 'more\nthings'    # single quote works too

print(a, b)

# output:

# this
# that
#  more
# things

To quote a string of multiple lines, use triple quotes. Example:

# python 3

d = """this
will be printed
in 3 lines"""

print(d)

# output:

# this
# will be printed
# in 3 lines

You can add r in front of the quote symbol. This way, backslash characters will be interpreted as is, not as escapes. (“r” for “raw”)

# python 3

c = r"this \n and that"

print(c) # prints a single line

Summary:

If you have a question, put $5 at patreon and message me.

Python 3

Lang Detail

  1. Print Version String
  2. Quoting String
  3. String Operations
  4. Format String
  5. Operators
  6. Object, ID, Type

Misc

  1. Traverse Directory
  2. Sort List, Matrix, Object
  3. Python 3: Map with Side Effect Doesn't Work If Result is Not Used
  4. Python 3 Closure
  5. Python 2 and 3 Difference