Python TutorialLesson 33Free preview
Python String Formatting
This lesson is part of Python · All levels
Summary
Python provides several ways to format strings so that the value of the variable is seamlessly embedded in the text, namely the f-string, the format() method, and the percent operator. The F-string, preceded by the letter f before the quotation marks, is the most modern and concise way. In addition to inserting values, formatting is also used to arrange decimal numbers, column widths, and the thousandth divisor.
Key points
- The F-string is written as f"Value {variable}" and is most recommended for Python 3.6 and above.
- The format method uses placeholder curly braces, for example "{} from {}".format(a, b).
- The F-string supports specifier formats such as f"{value: .2f}" for two decimals with a thousand divisor.
- The text sequence uses the signs > (right), < (left), and ^ (center) inside the placeholder.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.