String Formatting Python

String Formatting Python


  • We can use the format method on strings to concatenate and format strings in all kinds of powerful ways. To do this, create a string containing curly brackets. {}, as a place holder, to be replaced.
  • Then call the format method on the string using .format() and pass variables as parameters. The variables passed to the method will then be used to replace the curly bracket placeholders. This method automatically handles any conversion between data types for us.
  • If the curly brackets are empty, they’ll be populated with the variables passed in the order in which they’re passed.
  • However, you can put certain expressions inside the curly brackets to do even more powerful string formatting operations.
  • We can put the name of a variable into the curly brackets, then use the names in the parameters. This allows for more easily readable code, and for more flexibility with the order of variables.
  • We can also put a formatting expression inside the curly brackets, which lets us alter the way the string is formatted.
  • For example, the formatting expression {:2f} means that you’d format this as a float number, with two digits after the decimal dot.
  • The colon acts as a separator from the field name if you had specified one.
  • We can also specify text alignment using the greater than operator:>.
  • For example, the expression{:>.2f} would align the text three spaces to the right, as well as specify a float number with two decimal places.
  • String formatting can be very handy for outputting easy-to-read textual output.

Basic formatting with format()

The format() the method permits the use of simple placeholders for formatting.


# default arguments
print("Hello {}, your balance is {}.".format("Adam", 230.2346))

# positional arguments
print("Hello {0}, your balance is {1}.".format("Adam", 230.2346))

# keyword arguments
print("Hello {name}, your balance is {blc}.".format(name="Adam", blc=230.2346))

# mixed arguments
print("Hello {0}, your balance is {blc}.".format("Adam", blc=230.2346))
Hello Adam, your balance is 230.2346.
Hello Adam, your balance is 230.2346.
Hello Adam, your balance is 230.2346.
Hello Adam, your balance is 230.2346.

Simple number formatting

# integer arguments
print("The number is:{:d}".format(123))

# float arguments
print("The float number is:{:f}".format(123.4567898))

# octal, binary and hexadecimal format
print("bin: {0:b}, oct: {0:o}, hex: {0:x}".format(12))
The number is: 123
The number is:123.456790
bin: 1100, oct: 14, hex: c

Number formatting with padding for int and floats

# integer numbers with minimum width

# width doesn't work for numbers longer than padding

# padding for float numbers

# integer numbers with minimum width filled with zeros

# padding for float numbers filled with zeros
Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 421

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top