Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python format() method: formatted output

In this tutorial, we'll learn about the format() method in Python, which is a string method that helps you create formatted strings by replacing placeholders with specified values. The format() method is useful when you want to combine variables and static text in a single string in a clean and readable way.

The format() method takes any number of arguments, which can be positional or keyword arguments. The placeholders in the string are represented by curly braces {} and can include optional formatting options.

Example 1: Basic usage of the format() method

name = "John"
age = 25

text = "My name is {} and I am {} years old.".format(name, age)
print(text)  # Output: My name is John and I am 25 years old.

Example 2: Using positional arguments

You can specify the order in which the values should replace the placeholders by providing indices inside the curly braces.

name = "John"
age = 25

text = "I am {1} years old. My name is {0}.".format(name, age)
print(text)  # Output: I am 25 years old. My name is John.

Example 3: Using keyword arguments

You can use keyword arguments to provide values for the placeholders. This can make the code more readable, as the placeholders indicate the intended value.

name = "John"
age = 25

text = "My name is {name} and I am {age} years old.".format(name=name, age=age)
print(text)  # Output: My name is John and I am 25 years old.

Example 4: Formatting options

You can include formatting options inside the curly braces to control the presentation of the value. For example, you can set the number of decimal places for a floating-point number or control the alignment of the value in a fixed-width field.

pi = 3.14159265
formatted_pi = "{:.2f}".format(pi)
print(formatted_pi)  # Output: 3.14

name = "John"
formatted_name = "{:<10}".format(name)  # Left-align in a 10-character field
print(formatted_name)  # Output: John

In summary, the format() method allows you to create formatted strings by replacing placeholders with specified values. You can use positional or keyword arguments to provide the values and include optional formatting options inside the curly braces to control the presentation of the values. The format() method is a powerful and flexible way to create formatted strings in Python.

  1. How to Use format() for String Formatting in Python:

    • The format() method is used to format strings by replacing placeholders with values.
    # Example
    name = "Alice"
    age = 25
    message = "My name is {} and I am {} years old.".format(name, age)
    
  2. String Interpolation with format() in Python:

    • format() allows for string interpolation by positioning values within placeholders.
    # Example
    item = "book"
    price = 20.50
    invoice = "Item: {}, Price: ${:.2f}".format(item, price)
    
  3. Advanced Formatting with the format() Method:

    • format() supports advanced formatting options, including alignment, width, and precision.
    # Example
    value = 123.45678
    formatted_value = "Formatted: {:^10.2f}".format(value)
    
  4. Positional and Keyword Arguments in Python format():

    • Use positional and keyword arguments to specify the order of values in the format string.
    # Example
    first_name = "John"
    last_name = "Doe"
    full_name = "{}, {}".format(last_name, first_name)
    
  5. Numeric and String Formatting Using format() in Python:

    • Format numbers and strings with the format() method.
    # Example
    pi_value = 3.14159
    formatted_pi = "Formatted Pi: {:.2f}".format(pi_value)
    
  6. Formatting Variables and Expressions with format() in Python:

    • Include variables and expressions directly within the format string.
    # Example
    x = 10
    y = 20
    result = "Result: {} + {} = {}".format(x, y, x + y)
    
  7. Using format() with Alignment and Width in Python:

    • Specify alignment and width for formatted output using format().
    # Example
    city = "Paris"
    formatted_city = "City: {:>10}".format(city)
    
  8. Python f-strings vs format() Method for Formatting:

    • Compare f-strings and format() for string formatting in Python.
    # Example with f-string
    name = "Bob"
    age = 30
    message_fstring = f"My name is {name} and I am {age} years old."
    
    # Example with format()
    message_format = "My name is {} and I am {} years old.".format(name, age)
    
  9. Format Specification Language in Python format():

    • The format specification language allows fine-grained control over formatting.
    # Example
    value = 12345.6789
    formatted_value = "Formatted: {:,.2f}".format(value)