Python escape in string format

Python: How to print literal curly brace < or >in f-string and format string

Formatted string literals or f-string introduced in Python 3.6.

A formatted string literal or f-string is a string literal that is prefixed with ‘f’ or ‘F’. These strings may contain replacement fields, which are expressions delimited by curly braces <>. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.

If you want to print literal curly brace without any escape, you will get following:

>>> print(f"") File "", line 1 print(f"") ^ SyntaxError: f-string: expecting '>' >>> print(f">") File "", line 1 print(f">") ^ SyntaxError: f-string: single '>' is not allowed 

\ can not be used escape, you will get SyntaxError error:

>>> print(f"\") File "", line 1 print(f"\") ^ SyntaxError: f-string: expecting '>' >>> print(f"\>") File "", line 1 print(f"\>") ^ SyntaxError: f-string: single '>' is not allowed 

According f-string definition:

Escape sequences are decoded like in ordinary string literals (except when a literal is also marked as a raw string). After decoding, the grammar for the contents of the string is:

f_string ::= (literal_char | ">" | replacement_field)* replacement_field ::= "" f_expression ::= (conditional_expression | "*" or_expr) ("," conditional_expression | "," "*" or_expr)* [","] | yield_expression conversion ::= "s" | "r" | "a" format_spec ::= (literal_char | NULL | replacement_field)* literal_char ::= " or NULL> 

The parts of the string outside curly braces are treated literally, except that any doubled curly braces ‘>’ are replaced with the corresponding single curly brace. A single opening curly bracket ‘

>>> print(f"")  >>> print(f">>") > >>> print(f">") <> >>> print(f"foo>>") foo> 

This trick also apply to format string:

Format strings contain “replacement fields” surrounded by curly braces <>. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: >.

This following example is example how to print literal curly brace ‘

>>> print(" <>".format("Foo")) Traceback (most recent call last): File "", line 1, in module> ValueError: unexpected ' in field name >>> print("< <>".format("Foo"))  Foo >>> print(">> <>".format("Foo")) > Foo >>> print(">>> <>".format("Foo")) > Foo >>> print("<<>>> <>".format("Foo")) Traceback (most recent call last): File "", line 1, in module> IndexError: Replacement index 1 out of range for positional args tuple 

References

OmniLock — Block / Hide App on iOS

Block distractive apps from appearing on the Home Screen and App Library, enhance your focus and reduce screen time.

DNS Firewall for iOS and Mac OS

Encrypted your DNS to protect your privacy and firewall to block phishing, malicious domains, block ads in all browsers and apps

Источник

Python f-string

Python f-string tutorial shows how to format strings in Python with f-string.

Python f-string

Python f-string is the newest Python syntax to do string formatting. It is available since Python 3.6. Python f-strings provide a faster, more readable, more concise, and less error prone way of formatting strings in Python.

The f-strings have the f prefix and use <> brackets to evaluate values.

Format specifiers for types, padding, or aligning are specified after the colon character; for instance: f» , where price is a variable name.

Python string formatting

The following example summarizes string formatting options in Python.

#!/usr/bin/python name = 'Peter' age = 23 print('%s is %d years old' % (name, age)) print('<> is <> years old'.format(name, age)) print(f' is years old')

The example formats a string using two variables.

print('%s is %d years old' % (name, age))

This is the oldest option. It uses the % operator and classic string format specifies such as %s and %d .

print('<> is <> years old'.format(name, age))

Since Python 3.0, the format function was introduced to provide advance formatting options.

Python f-strings are available since Python 3.6. The string has the f prefix and uses <> to evaluate variables.

$ python formatting_string.py Peter is 23 years old Peter is 23 years old Peter is 23 years old

Python f-string expressions

We can put expressions between the <> brackets.

#!/usr/bin/python bags = 3 apples_in_bag = 12 print(f'There are total of apples')

The example evaluates an expression inside f-string.

$ python expressions.py There are total of 36 apples

Python f-string dictionaries

We can work with dictionaries in f-strings.

#!/usr/bin/python user = print(f" is a ")

The example evaluates a dictionary in an f-string.

$ python dicts.py John Doe is a gardener

Python f-string debug

Python 3.8 introduced the self-documenting expression with the = character.

#!/usr/bin/python import math x = 0.8 print(f'') print(f'')

The example outputs the Sine and Cosine functions in the debug mode.

$ python debug.py math.cos(x) = 0.6967067093471654 math.sin(x) = 0.7173560908995228

Python multiline f-string

We can work with multiline strings.

#!/usr/bin/python name = 'John Doe' age = 32 occupation = 'gardener' msg = ( f'Name: \n' f'Age: \n' f'Occupation: ' ) print(msg)

The example presents a multiline f-string. The f-strings are placed between round brackets; each of the strings is preceded with the f character.

$ python multiline.py Name: John Doe Age: 32 Occupation: gardener

Python f-string calling function

We can also call functions in f-strings.

#!/usr/bin/python def mymax(x, y): return x if x > y else y a = 3 b = 4 print(f'Max of and is ')

The example calls a custom function in the f-string.

$ python call_fun.py Max of 3 and 4 is 4

Python f-string objects

Python f-string accepts objects as well; the objects must have either __str__ or __repr__ magic functions defined.

#!/usr/bin/python class User: def __init__(self, name, occupation): self.name = name self.occupation = occupation def __repr__(self): return f" is a " u = User('John Doe', 'gardener') print(f'')

The example evaluates an object in the f-string.

$ python objects.py John Doe is a gardener

Python f-string escaping characters

The following example shows how to escape certain characters in f-strings.

#!/usr/bin/python print(f'Python uses > to evaludate variables in f-strings') print(f'This was a \'great\' film')

To escape a curly bracket, we double the character. A single quote is escaped with a backslash character.

$ python escaping.py Python uses <> to evaludate variables in f-strings This was a 'great' film

Python f-string format datetime

The following example formats datetime.

#!/usr/bin/python import datetime now = datetime.datetime.now() print(f'') print(f'') print(f'') print(f'') print(f'') print(f'')

The example displays a formatted current datetime. The datetime format specifiers follow the : character.

$ python format_datetime.py 2023-03-24 15:12 Friday Fri March 083 5

Python f-string format floats

Floating point values have the f suffix. We can also specify the precision: the number of decimal places. The precision is a value that goes right after the dot character.

#!/usr/bin/python val = 12.3 print(f'') print(f'')

The example prints a formatted floating point value.

$ python format_floats.py 12.30 12.30000

The output shows the number having two and five decimal places.

Python f-string percentage

We display a decimal value as percentage using the % format specifier.

#!/usr/bin/python val = 1/7.0 print(f'') print(f'')

In the program, we display the 1/7 value as a percentage with two decimal places.

$ python percentage.py 0.14285714285714285 14.29%

Python f-string format width

The width specifier sets the width of the value. The value may be filled with spaces or other characters if the value is shorter than the specified width.

#!/usr/bin/python for x in range(1, 11): print(f'  ')

The example prints three columns. Each of the columns has a predefined width. The first column uses 0 to fill shorter values.

$ python format_width.py 01 1 1 02 4 8 03 9 27 04 16 64 05 25 125 06 36 216 07 49 343 08 64 512 09 81 729 10 100 1000

Python f-string justify string

By default, the strings are justified to the left. We can use the > character to justify the strings to the right. The > character follows the colon character.

#!/usr/bin/python s1 = 'a' s2 = 'ab' s3 = 'abc' s4 = 'abcd' print(f'10>') print(f'10>') print(f'10>') print(f'10>')

We have four strings of different length. We set the width of the output to ten characters. The values are justified to the right.

$ python justify.py a ab abc abcd

Python f-string numeric notations

Numbers can have various numeric notations, such as decadic or hexadecimal.

#!/usr/bin/python val = 300 # hexadecimal lower print(f'') # hexadecimal upper print(f'') # octal print(f'') # binary print(f'') # scientific print(f'')

The example prints a value in various notations.

$ python format_notations.py 12c 12C 454 100101100 3.000000e+02

In this tutorial we have worked with Python f-strings.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

Источник

Читайте также:  Coding games using java
Оцените статью