Lecture
Comparison of format() Function and f-Strings
The f-string and format() function are two useful methods for string formatting in Python.
Let's summarize how each syntax is used and what characteristics they have.
format Function
The format() function inserts variables into a string using curly braces {}.
Example of using format() function
name = "CodeFriends" age = 20 # Using the format() function message = "My name is {} and I am {} years old.".format(name, age) print(message)
You can specify the order of variables using {index}.
Example of using index with format() function
"I am {1} years old and my name is {0}.".format(name, age) # Output: I am 20 years old and my name is CodeFriends.
f-Strings
f-strings use f or F before the string and allow you to include variables or expressions directly inside curly braces {}.
name = "CodeFriends" age = 20 # Using f-strings message = f"My name is {name} and I am {age} years old." print(message)
Features:
- You can include variable names, expressions, and function calls directly inside the curly braces
{}. - It is concise and improves code readability.
width = 5 height = 3 print(f"The area of the rectangle is {width * height}.") # Output: The area of the rectangle is 15.
Comparison of format() Function and f-Strings
| Feature | format() Function | f-Strings |
|---|---|---|
| Python Version | Python 2.7 and above | Python 3.6 and above |
| Usage | Requires .format() function call | Use f before the string and curly braces {} |
| Expressions | Not possible (only variables) | Possible (arithmetic operations, function calls, etc.) |
| Flexibility | Suitable for dynamic formatting, where the number or position of variables may change | Suitable for simple formatting and quick expressions |
Tip: For most modern Python projects,
f-stringsare preferred for their clarity and simplicity.
Previous lessonHow to Format Strings with f-StringsNext lessonUsing the `input` Function to Receive User Input
Lessons in this chapter · String Formatting Made Easy
- 1. Inserting Variables into Strings with the format() Function
- 2. Tips for Using `format()` Function - indexError
- 3. Displaying Integers with the format() Function
- 4. Handling Floating-Point Numbers with the format() Function
- 5. Multiple-choice quiz
- 6. Case Conversion Methods upper() and lower()
- 7. Removing Leading and Trailing Whitespace with the strip() Function
- 8. String Composition Verification with isOOO() Methods
- 9. Finding the Position of a Specific Character Using find() and rfind()
- 10. Using the split() Method for Strings
- 11. How to Check if a Specific Character Exists in a String
- 12. Multiple-choice quiz
- 13. How to Format Strings with f-Strings
- 14. Comparison of format() Function and f-Strings
- 15. Using the `input` Function to Receive User Input
- 16. Indicating Invalid Values with ValueError
- 17. Coding Quiz - Splitting Strings
- 18. Multiple-choice quiz
- 19. Fill-in-the-blank quiz
Quiz
0 / 1
What is the most appropriate option to fill in the blank?
When you need to insert expressions like arithmetic operations within a string, it is better to use .
format() function
f-string
split() function
None of the above
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help