|
1 |
| -# Lesson-103-python-formating-strings |
| 1 | +# Lesson 103: Python String Formatting |
| 2 | + |
| 3 | +Welcome to Lesson 103 of the Python Learning Lounge! In this lesson, we will cover various methods for formatting strings in Python. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Introduction](#introduction) |
| 8 | +- [String Formatting Methods](#string-formatting-methods) |
| 9 | + - [Old Style `%` Formatting](#old-style--formatting) |
| 10 | + - [New Style `.format()` Method](#new-style-format-method) |
| 11 | + - [f-Strings (Literal String Interpolation)](#f-strings-literal-string-interpolation) |
| 12 | +- [Examples](#examples) |
| 13 | +- [Exercises](#exercises) |
| 14 | +- [Conclusion](#conclusion) |
| 15 | + |
| 16 | +## Introduction |
| 17 | + |
| 18 | +String formatting is a crucial part of writing clean and readable code in Python. It allows you to create strings that include variable values, making your code more dynamic and versatile. |
| 19 | + |
| 20 | +## String Formatting Methods |
| 21 | + |
| 22 | +### Old Style `%` Formatting |
| 23 | + |
| 24 | +The old style of string formatting uses the `%` operator. This method is less commonly used in modern Python code but is still worth knowing. |
| 25 | + |
| 26 | +```python |
| 27 | +name = "Alice" |
| 28 | +age = 30 |
| 29 | +formatted_string = "Hello, %s! You are %d years old." % (name, age) |
| 30 | +print(formatted_string) |
| 31 | +``` |
| 32 | + |
| 33 | +### New Style `.format()` Method |
| 34 | + |
| 35 | +The `.format()` method is more powerful and flexible than the old style. It uses curly braces `{}` as placeholders in the string. |
| 36 | + |
| 37 | +```python |
| 38 | +name = "Alice" |
| 39 | +age = 30 |
| 40 | +formatted_string = "Hello, {}! You are {} years old.".format(name, age) |
| 41 | +print(formatted_string) |
| 42 | +``` |
| 43 | + |
| 44 | +### f-Strings (Literal String Interpolation) |
| 45 | + |
| 46 | +Introduced in Python 3.6, f-strings are the most modern and preferred way of formatting strings. They provide a concise and readable syntax. |
| 47 | + |
| 48 | +```python |
| 49 | +name = "Alice" |
| 50 | +age = 30 |
| 51 | +formatted_string = f"Hello, {name}! You are {age} years old." |
| 52 | +print(formatted_string) |
| 53 | +``` |
| 54 | + |
| 55 | +## Examples |
| 56 | + |
| 57 | +Here are a few more examples of string formatting: |
| 58 | + |
| 59 | +1. Using the old style `%` formatting: |
| 60 | + |
| 61 | + ```python |
| 62 | + value = 42 |
| 63 | + formatted_string = "The answer is %d." % value |
| 64 | + print(formatted_string) |
| 65 | + ``` |
| 66 | + |
| 67 | +2. Using the `.format()` method: |
| 68 | + |
| 69 | + ```python |
| 70 | + temperature = 20.5 |
| 71 | + formatted_string = "The temperature is {:.1f} degrees Celsius.".format(temperature) |
| 72 | + print(formatted_string) |
| 73 | + ``` |
| 74 | + |
| 75 | +3. Using f-strings: |
| 76 | + |
| 77 | + ```python |
| 78 | + pi = 3.14159 |
| 79 | + formatted_string = f"The value of pi is approximately {pi:.2f}." |
| 80 | + print(formatted_string) |
| 81 | + ``` |
| 82 | + |
| 83 | +## Exercises |
| 84 | + |
| 85 | +1. Write a string that includes your name and your favorite hobby using the old style `%` formatting. |
| 86 | +2. Create a string that includes your age and the current year using the `.format()` method. |
| 87 | +3. Use an f-string to create a string that includes your hometown and your favorite food. |
| 88 | + |
| 89 | +## Conclusion |
| 90 | + |
| 91 | +String formatting is a fundamental skill for any Python programmer. By mastering these three methods, you'll be well-equipped to handle any string formatting task that comes your way. |
| 92 | + |
| 93 | +Happy coding! |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +For more lessons and tutorials, visit the [Python Learning Lounge](https://github.com/Python-leatning-lounge). |
0 commit comments