HomeSearch

Python String Literal: F, R Strings

Use string literals with triple quotes and raw string literals. Add and multiply strings.
Literals. In Python programs we often specify strings directly. This is a string literal: a string that is part of the program itself (in quotes).Strings
Triple-quoted. This literal syntax form begins and ends with three quotes. Newlines are left as is. In triple-quoted syntax, we do not need to escape quotes.

Note: In this syntax, the Python interpreter scans for three quotes for the start and end of the literal. A single quote can also be used.

Note 2: We must surround a triple-quoted string with the same kind of quotes. Double quotes must be matched with double quotes.

Python program that uses triple-quotes # Use a triple-quoted string. v = """This string has triple "quotes" on it.""" print(v) Output This string has triple "quotes" on it.
Parentheses. We can place parentheses around string literals on separate lines to combine them into one string literal. This syntax is an alternative to triple-quoted in certain cases.

Usually: It is best to use standard syntax like triple-quoted strings. Here we combine three string literals into one.

Python program that uses parentheses around literals # Surround string literals with parentheses to combine them. lines = ("cat " "bird " "frog") print(lines) Output cat bird frog
Raw literals. By prefixing a string literal with an r, we specify a raw string. In a raw string, the backslash character does not specify an escape sequence—it is a regular character.

Tip: Raw string literals are ideal for regular expression patterns. In "re" we often use the backslash.

Here: The "123" is treated as an escaped sequence in the normal string literal, but not in the raw one.

Paths: For file paths or folder paths, raw string literals are ideal. They eliminate confusion about slashes or backslashes on Windows.

Path
Python program that uses raw string # In a raw string "\" characters do not escape. raw = r"\directory\123" val = "\directory\123" print(raw) print(val) Output \directory\123 \directoryS
Format literal, f. We can prefix a string with the "f" character to specify a format literal. Variables in the current scope are placed inside the curly brackets surrounding their names.format
Python program that uses format literals brick_color = "red" brick_size = 50 # Use format literal. result = f"the brick weighs {brick_size} and is {brick_color}" print(result) Output the brick weighs 50 and is red
Add, multiply. A string can be added to another string. And it can be multiplied by a number. This copies that same string into a new string the specified number of times.

Caution: If you multiply a string against a large number, you may get a MemoryError. An if-check to prevent this may be helpful.

Python program that adds, multiplies strings s = "abc?" # Add two strings together. add = s + s print(add) # Multiply a string. product = s * 3 print(product) Output abc?abc? abc?abc?abc?
A review. String literals are everywhere in Python programs. For file paths, raw literals (with r) are an ideal syntax. We rarely multiply strings, but this too is possible.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls