String Formatting In Python
In this lesson, we discuss Python string formatting. We will use examples to better serve the needs of the learners in today’s lesson.
The format() method in Python can be used to format a string so that it displays as expected.
Python String Formatting
When you use Python string formatting OR the format() method, you can format selected parts of a string.
Some parts of a text are not under your control. Maybe they come from a database or user input?
You can control such values by adding placeholders curly brackets {} and calling the format() method.
You can display the pi value by inserting a placeholder:
Python String Formatting Example:1 
By inserting a placeholder, you can print the age and height values:
Python String Formatting Example:2 
Indicate how to modify the value by including parameters inside the curly brackets.
To display pi as a three-digit number, format the value as follows:
Python String Formatting Example:3 
Show the subjects marks as a number with one decimal place:
Python String Formatting Example:4 
Index Numbers
In order to ensure that the values are placed correctly, you can utilize index numbers (the number within the curly brackets {0}):
Index Numbers Example:1 
Index Numbers Example:2 
If you need the exact value more than once, apply index number:
Index Numbers Example:3 
Named Indexes
If you want to apply named indexes, include a name inside the curly brackets {name}, but then include names when passing the parameters to txt.format(name = “Elon Musk”):
Named Indexes Example:1 
Include the following phone specification variables inside curly brackets{} in mrx_info:
Example: 
Multiple Values
You can pass more values to the format() method if you wish:
print(txt.format(sub_1, sub_2, sub_3, sub_4))
You can also insert more placeholders:
String Format Multiple Value Example:1 
String Format Multiple Value Example:2 
Python String Formatting Advantages
Here are some advantages of using string formatting in Python:
- String formatting improves code readability by allowing you to separate the formatting logic from the content of the string. Instead of concatenating strings manually, you can use placeholders or format specifiers to define the structure of the output. This makes the code more concise, easier to understand, and maintainable.
- String formatting allows you to dynamically insert values into the output string. You can include variables, expressions, or function calls within the format placeholders. This enables you to generate output that incorporates dynamic data, calculations, or results from other parts of your program.
- Python string formatting provides various formatting options to control the appearance of the output. You can specify the width, precision, alignment, padding, and other formatting attributes for numeric values, strings, dates, and more. This allows you to present data in a specific format that meets your requirements.
- String formatting supports localization and internationalization efforts. It enables you to format numbers, dates, and currencies according to different regional conventions and language preferences. This flexibility ensures that your program can present output in a way that is culturally appropriate and understandable for different audiences.
- By using string formatting, you can create reusable templates with placeholders that can be populated with different values. This allows you to define a template once and reuse it multiple times with different inputs, reducing code duplication and improving code organization.