Open links in new tab
  1. The str.format() method in Python allows you to insert variables or values into a string using placeholders {}, making it a powerful tool for creating dynamic and well-formatted output.

    Example:

    name = "Alice"
    age = 30
    print("My name is {} and I am {} years old.".format(name, age))
    # Output: My name is Alice and I am 30 years old
    Copied!

    1. Positional and Keyword Arguments You can reference placeholders by position or by name.

    print("{1} loves {0}!".format("Python", "Alice"))
    # Output: Alice loves Python!

    print("{lang} is fun!".format(lang="Python"))
    # Output: Python is fun!
    Copied!

    This improves readability and flexibility when formatting strings.

    2. Number Formatting & Alignment The : inside {} lets you control alignment, width, precision, and type.

    num = 1234.56789
    print("{:<15}".format("left")) # Left align
    print("{:>15}".format("right")) # Right align
    print("{:^15}".format("center")) # Center align
    print("{:.2f}".format(num)) # 2 decimal places
    print("{:,}".format(1000000)) # Thousand separator
    Copied!

    Output:

    left
    right
    center
    1234.57
    1,000,000
    Copied!
  2. Sponsored
    Learn Python
    www.coursera.org/ibm/data-analyst
    About our ads
    Earn a Professional Certificate from IBM in as little as 5 months—no experience required.
    Python String format () Method - W3Schools

    Learn how to use the format() method to format and insert values into a string. See examples of different placeholders, formatting types and syntax.

    W3School
    PyFormat: Using % and .format () for great good!

    Python has had awesome string formatters for many years but the documentation on them is far too theoretic and technical. With this site we try to show you the most com…

    PyFormat
    string — Common string operations — Python 3.14.3 documentation

    Learn how to use the string module to perform common string operations and customize string formatting in Python. See the syntax, methods, constants and exam…

    Python
    format () | Python’s Built-in Functions – Real Python

    The built-in format() function converts an input value into a formatted string representation based on the specified format. The function takes a format speci…

    Real Python
    • Preply
      preply.com › Language › Classes
      About our ads

      Python Tutors Online - Learn at Preply

      SponsoredGet 1-on-1 lessons to reach your language goals this year. Personalized language lessons = the better way to learn.

      Patient and responsive · Learn at home · Certified TESOL teacher

      4/5 (18K reviews)Site visitors: Over 10K in the past month
  1. PyFormat: Using % and .format () for great good!

    Python has had awesome string formatters for many years but the documentation on them is far too theoretic and technical. With this site we try to show you the most common use-cases covered by the …

  2. string — Common string operations — Python 3.14.3 documentation

      • String constants¶ The constants defined in this module are: string.ascii_letters¶ …
      • Custom String Formatting¶ The built-in string class provides the ability to do …
      • Format String Syntax¶ The str.format() method and the Formatter class share …
      • Template strings¶ Template strings provide simpler string substitutions as …
      • Helper functions¶ string.capwords(s, sep=None)¶ Split the argument into words …
  3. format () | Python’s Built-in Functions – Real Python

    The built-in format() function converts an input value into a formatted string representation based on the specified format. The function takes a format …

  4. Python String format () Method - GeeksforGeeks

    Jul 11, 2025 · format () method in Python is a tool used to create formatted strings. By embedding variables or values into placeholders within a template string, we can construct dynamic, well …

  5. Python Format Output Guide: Strings & Numbers - PyTutorial

    Feb 19, 2026 · Learn how to format output in Python using f-strings, format (), and %-formatting for clean, readable text and data presentation.

  6. Python Format Function - Online Tutorials Library

    Learn how to use the Python format function to format strings effectively with various techniques and examples.

  7. Formatting Text in Python: A Complete Guide to String Manipulation

    Learn how to format text effectively in Python using various string manipulation techniques like f-strings, format (), and more. Perfect for beginners and advanced users.

  8. Python String format () - Programiz

    Learn how to use the format() method to format strings, numbers and other types in Python. See examples of positional, keyword and basic formatting, and different number formats with specifiers.

  9. String Formatting in Python - GeeksforGeeks

    Mar 18, 2026 · String formatting in Python is used to insert variables and expressions into strings in a readable and structured way. It helps create dynamic output and …

  10. People also ask
    Loading
    Unable to load answer