Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Input and Output

Format and align strings using format() - Python Program

Example 1: Basic Program

name = "Ronnie" score = 98 print("Name: {:<10} Score: {:>5}".format(name, score))

Output

 
OUTPUT  :
Name: Ronnie     Score:    98 

: <10 left-aligns, : >5 right-aligns for neat columnar output.

Example 2: Advanced Program

# --- Basic Formatting --- name = "Ronnie" age = 21 message_basic = "My name is {} and I am {} years old.".format(name, age) print(f"Basic Formatting: {message_basic}") # --- Positional Arguments --- item = "book" price = 19.99 message_positional = "The {1} costs ${0:.2f}.".format(price, item) print(f"Positional Arguments: {message_positional}") # --- Keyword Arguments --- city = "New York" country = "USA" message_keyword = "I live in {city}, {country}.".format(city=city, country=country) print(f"Keyword Arguments: {message_keyword}") # --- String Alignment and Padding --- text = "Python" width = 20 # Left Alignment left_aligned = "{:<{}}".format(text, width) print(f"Left Aligned: '{left_aligned}'") # Right Alignment right_aligned = "{:>{}}".format(text, width) print(f"Right Aligned: '{right_aligned}'") # Center Alignment center_aligned = "{:^{}}".format(text, width) print(f"Center Aligned: '{center_aligned}'") # Padding with a specific character padded_char = "{:*^{}}".format(text, width) print(f"Padded with '*': '{padded_char}'") # --- Number Formatting --- value_float = 123.456789 value_int = 123456789 # Limiting decimal places formatted_float = "Value: {:.2f}".format(value_float) print(f"Formatted Float (2 decimal places): {formatted_float}") # Thousands Separator formatted_int = "Large Number: {:,}".format(value_int) print(f"Formatted Integer (thousands separator): {formatted_int}") # Percentage Formatting percentage = 0.75 formatted_percent = "Progress: {:.1%}".format(percentage) print(f"Formatted Percentage: {formatted_percent}") # --- Combining Formatting Options --- product = "Laptop" cost = 1200.50 stock = 15 combined_format = "{:<15} | ${:>10.2f} | Stock: {:>5}".format(product, cost, stock) print(f"Combined Formatting: {combined_format}")

Output

 
OUTPUT  :
Basic Formatting: My name is Ronnie and I am 21 years old.
Positional Arguments: The book costs $19.99.
Keyword Arguments: I live in New York, USA.
Left Aligned: 'Python              '
Right Aligned: '              Python'
Center Aligned: '       Python       '
Padded with '*': '*******Python*******'
Formatted Float (2 decimal places): Value: 123.46
Formatted Integer (thousands separator): Large Number: 123,456,789
Formatted Percentage: Progress: 75.0%
Combined Formatting: Laptop          | $   1200.50 | Stock:    15

Explanation:

  • Basic Formatting:

Simplest use, placeholders are filled in order.

  • Positional Arguments:

Use index numbers within {}, e.g., {0}, {1}, to specify which argument to use.

  • Keyword Arguments:

Use names within {}, e.g., {city}, {country}, and pass arguments as keyword arguments to format().

  • String Alignment and Padding:
    • <: Left-aligns the text within the specified width.
    • >: Right-aligns the text within the specified width.
    • ^: Centers the text within the specified width.
    • You can add a padding character before the alignment specifier (e.g., :*^).
  • Number Formatting:
    • :.2f: Formats a float to two decimal places.
    • :,: Adds a thousands separator to an integer.
    • :.1%: Formats a float as a percentage with one decimal place.
  • Combining Formatting Options:

Multiple format specifiers can be used within a single placeholder to control alignment, precision, and other aspects simultaneously. The width is specified after the alignment character.