You are currently viewing Python cheat sheet 105: Formatted Strings in Python

Python cheat sheet 105: Formatted Strings in Python

Python is a versatile and beginner-friendly programming language that offers various ways to manipulate and formatted strings. One essential aspect of Python programming is working with formatted strings. Formatted strings allow you to insert variables or expressions into a string while controlling the format and alignment of the output. In this tutorial, we will explore the basics of formatted strings in Python and demonstrate their usage with practical examples.

What are Formatted Strings in python?

Formatted strings, also known as f-strings in Python, provide a concise and convenient way to embed variables and expressions within a string. They allow you to create dynamic text by substituting values into predefined placeholders. This feature is beneficial for generating output that varies based on program inputs or calculations.

Basic String Formatting

Let’s start with some basic string formatting using the % operator. This method is somewhat outdated but still functional.

name = "Alice"
age = 30

formatted_string = "My name is %s, and I am %d years old." % (name, age)
print(formatted_string)

Output:

My name is Alice, and I am 30 years old.

In this example, %s and %d are placeholders for string and integer values, respectively. The variables name and age are substituted into the placeholders.

Using f-strings

Starting from Python 3.6, f-strings were introduced as a more modern and readable way to format strings. Here’s how you can use them:

name = "Bob"
age = 25

formatted_string = f"My name is {name}, and I am {age} years old."
print(formatted_string)

Output:

My name is Bob, and I am 25 years old.

In this example, the f before the string denotes an f-string, and variables are enclosed within curly braces {}. Python replaces these expressions with their values when constructing the string.

Formatting with Placeholders

You can use placeholders to control the formatting of variables within a string. For instance, specifying the number of decimal places for a floating-point number:

price = 29.99

formatted_string = f"The price is: ${price:.2f}"
print(formatted_string)

Output:

The price is: $29.99

In this example, :.2f inside the curly braces tells Python to format the price variable as a floating-point number with two decimal places.

Using .format() method

Another way to format strings is by using the .format() method:

product = "Laptop"
price = 999.99

formatted_string = "Product: {}, Price: ${:.2f}".format(product, price)
print(formatted_string)

Output:

Product: Laptop, Price: $999.99

In this case, {} are placeholders, and the .format() method replaces them with the provided values. You can also specify the index of the variables if needed.

Conclusion

Formatted strings are a crucial tool for creating dynamic and well-structured output in Python. You can choose between different methods like f-strings, % formatting, or the .format() method based on your Python version and personal preference. Experiment with these techniques to effectively format your strings and make your Python code more expressive and readable.

In this tutorial, we covered the basics of formatted strings in Python, explaining the concepts and providing practical examples for each method. Now, you have the knowledge and skills to start using formatted strings in your Python programs effectively. Happy coding!

Remember to practice these concepts to reinforce your understanding of formatted strings in Python. As you gain experience, you will be able to use them efficiently in your programming projects.

Leave a Reply