Python String Formatting 🎯

beginner
20 min

Python String Formatting 🎯

Welcome to our comprehensive guide on String Formatting in Python! This tutorial is designed for both beginners and intermediate learners, so let's dive right in. 🏊‍♂️

What is String Formatting? 📝

String formatting is the process of modifying a string based on certain rules to include variables or placeholders. In Python, we have several methods for string formatting, but we'll focus on the older % formatting and the newer f-string formatting.

Older Method: % Formatting 💡

Let's start with the older method, % formatting.

python
# Define variables name = "John Doe" age = 25 # Use % formatting print("My name is %s and I am %i years old." % (name, age))

In the above example, %s is used for strings and %i is used for integers. The % operator is followed by the format specifier (s or i), and the variable is placed within parentheses, separated by spaces.

Quiz 📝

Question: What does %s represent in Python's % formatting?

A: String

B: Integer

C: Float

Correct: A

Explanation: %s is used to represent a string in Python's % formatting.

Newer Method: f-string Formatting 💡

Python 3.6 introduced a new method for string formatting, called f-string formatting. It is more readable and easier to use.

python
# Define variables name = "John Doe" age = 25 # Use f-string formatting print(f"My name is {name} and I am {age} years old.")

In the above example, we use f before the string, and place the variables within curly braces {}. This method is more flexible, as you can format multiple variables within the same string.

Quiz 📝

Question: What is the advantage of using f-string formatting over % formatting in Python?

A: It is more readable and easier to use.

B: It is faster.

C: It supports more format specifiers.

Correct: A

Explanation: f-string formatting is more readable and easier to use in Python. It is also faster and supports more format specifiers compared to % formatting.

Formatting Variables 💡

You can format variables in multiple ways using both methods. Here are some examples:

% Formatting

python
# Define variables name = "John Doe" age = 25 # Formatting with %s print("My name is %s and I am %i years old." % (name, age)) # Formatting with %f for float weight = 75.5 print("I weigh %0.2f kg." % weight)

f-string Formatting

python
# Define variables name = "John Doe" age = 25 weight = 75.5 # Formatting with f-strings print(f"My name is {name} and I am {age} years old.") print(f"I weigh {weight:.2f} kg.")

In the above examples, we use %0.2f for % formatting to format a float with two decimal places, and {weight:.2f} for f-string formatting to achieve the same result.

Conclusion 📝

Now you have a good understanding of string formatting in Python. Practice these methods and explore more format specifiers to make your code cleaner and more efficient. Happy coding! 🚀

Remember, consistency is key. Try to use f-string formatting whenever possible, as it is more readable and easier to use.

Quiz

Question: Which of the following is a correct way to format a float with two decimal places using f-string formatting?

A: {float:.2f}

B: {float:.2}

C: {float:.2f}

Correct: C

Explanation: To format a float with two decimal places using f-string formatting, you should use {float:.2f}.