SoFunction
Updated on 2024-11-16

Advanced usage of python3 formatting string f-string (recommended)

f-string, also known as formatted string literals, is a new string formatting method introduced in Python 3.6, which stems from PEP 498 - Literal String Interpolation, with the main goal of making the formatting strings easier.

An f-string is formally a string (f'xxx' or F'xxx') led by an f or F modifier, with curly braces {} identifying the field being replaced; an f-string is not essentially a string constant, but rather an expression that evaluates to a value at runtime:

While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.
(Unlike other string constants that have constant values, formatted strings are actually expressions that are evaluated by run-time operations.)
—— Python Documentation

f-string in terms of functionality is not inferior to the traditional %-formatting statement and () function, but at the same time performance is better than both, and the use of a more concise and clear, so for Python 3.6 and later, it is recommended that the use of f-string for string formatting.

As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less error-prone than other formatting methods, but they are also faster!

Prior to Python 3.6, there were two main methods for formatting Python expressions embedded in string text: %-formatting and ().

%-formatting

String objects have built-in operations using the % operator, which you can use to format strings.

name = "tom"
print("The cat's name is %s."%name)

Output:

cat name is tom

The sample code above looks readable enough, but once you start using multiple parameters and longer strings, the code becomes less readable.

name = "tom"
age = 2
action = "Promptly."
disposition = "Lazy."

print("The cat's name is %s, and he is %d years old, and he catches mice very %s,but is very %s, and always sleeps during the day."%(name, age, action, disposition))

Output:

The cat's name is tom and he is 2 years old today. He is very fast in catching mice, but he is very lazy and always sleeps during the day.

()

() is an improvement on %-formatting. It uses normal function call syntax and can be extended by __format __() methods on objects to be converted to strings.

Use (), with replacement fields marked by curly brackets:

name = "tom"
print("The cat's name is{}".format(name))

Output:

The cat's name is Tom.

Variables can also be referenced in any order by referencing their index:

name = "tom"
age = 2

print("The cat's name is{1},last year{0}It's a year old.,Today's the week.{0}".format(age, name))

Output:

Cat's name is Tom, he is 2 years old, today is Tuesday.

Formatting can also be done cleverly with dictionaries using **:

cat = {"name": "tom", "age": 2}

print("The cat's name is{name},last year{age}It's a year old.".format(**cat))

Output:

The cat's name is tom and he's 2 years old.

Formatting code with () is more readable than with %-formatting, but () still looks very verbose when dealing with multiple arguments and longer strings.

f-Strings: a new way to improve Python format strings

The f-Strings syntax is similar to the syntax used by (), but is less verbose in detail and only requires an f or F before the formatted string. See how easy and readable this is:

name = "tom"
age = 2

print(f"The cat's name is{name},last year{age}It's a year old.")

Output:

The cat's name is tom and he's 2 years old.

Functions can be used for formatting:

name = "tom"
age = 2

print(F"The cat's name is{()},last year{age}It's a year old.")

Output:

The cat's name is Tom and he's 2 years old.

Dictionaries can be used for formatting:

cat = {"name": "tom", "age": 2}

print(F"The cat's name is{cat['name']},last year{cat['age']}It's a year old.")

Output:

The cat's name is tom and he's 2 years old.

Formatting can be done with a limit on the floating-point precision:

name = "tom"
age = 2.1274

print(F"The cat's name is{name},last year{age: .2f}It's a year old.")

Output:

Cat's name is tom and he is 2.13 years old.

summarize

to this advanced use of python3 formatting string f-string (recommended) of the article is introduced to this, more related python f-string formatting string content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!