How to format text and what are f-strings in Python? (10/100 Days of Python)

Martin Mirakyan
4 min readJan 11, 2023

--

Day 10 of the “100 Days of Python” blog post series covering f-strings and how to format text in Python

Python provides several methods for formatting text. Whether you want to insert textual variables into a string or numeric values, there is a method that can help you do it. In this tutorial, we’ll cover 3 ways of formatting strings provided by Python among which the last one — f-strings is available since Python 3.6 and is the preferred way of formatting strings. We’ll also cover some ways of formatting numbers that are very useful when using numeric values and can be tricky to display nicely.

Using .format() method

The .format() method is a versatile way to insert variables into a string. It works by placing curly braces {} in the string, and then specifying the values to be inserted inside the braces:

name = 'Anna'
print('Hello, {}!'.format(name)) # Will print `Hello, Anna!`

You can also specify the index of the value to be inserted:

name = 'Anna'
age = 25
print('{0} is {1} years old.'.format(name, age)) # Anna is 25 years old.
print('{1} is {0} years old.'.format(age, name)) # Anna is 25 years old.

Here both of the print statements print the same output as the order of variables in .format() is specified in the string with their index.

You can also use keywords to specify the values to be inserted:

name = 'Anna'
age = 25
print('{name} is {age} years old. {name} is very smart!'.format(name=name, age=age))
# Will print `Anna is 25 years old. Anna is very smart!`

Using the % operator

The % operator is another way to format strings in Python. It works by placing a % symbol followed by a format specifier, and then the value to be formatted:

name = 'Anna'
print('Hello, %s!' % name) # Will print `Hello, Anna!`

Note that you need to specify the type of a variable when using the % operator:

  • %s for strings
  • %d for integers
  • %f for floating-point numbers
  • %.5f for floating-point numbers with 5-digit precision
  • %x for integers in hex representation

You can also use multiple values and format specifiers:

name = 'Anna'
age = 25
print('%s is %d years old.' % (name, age)) # Anna is 25 years old.

Using f-strings

In Python 3.6 and later, you can use f-strings (formatted string literals) to insert variables into a string. An f-string starts with the letter f, followed by a string in single or double quotes. Inside the string, you can insert variables by enclosing them in curly braces {}:

name = 'Anna'
print(f'Hello, {name}!') # Will print `Hello, Anna!`

You can also use expressions inside the curly braces:

name = 'Anna'
age = 24
print(f'{name} is {age + 1} years old.') # Anna is 25 years old.

Formatting numbers

In addition to formatting text, you can also use these methods to format numbers. There are many utilities that help one format both integer and floating-point numbers properly:

price = 9.99
print(f'The price is ${price}') # `The price is $9.99`

pi = 3.14159
print(f'Pi is approximately {pi:.2f}') # `Pi is approximately 3.14`

x = 55
print(f'We can pad with zeros {x:04d}') # `We can pad with zeros 0055`
print(f'Or pad with spaces {x:8d}') # `Or pad with spaces 55`
print(f'Pad before with spaces {x:>8d}') # `Pad before with spaces 55`
print(f'Pad after with spaces {x:<8d}') # `Pad after with spaces 55 `
print(f'Pad center with spaces {x:^8d}') # `Pad center with spaces 55 `

Keep in mind that in f-strings, the formatting part comes after the : sign, while the variable that’s being formatted comes before it.

Note that in the last examples, 8 indicates that the value of x should take up a space of length 8, and the character later indicates the alignment of the value of x — as it doesn’t take up the whole length. The > sign indicates that the output should be justified right, while the < sign indicates that it should be justified left, and finally, the ^ sign indicates that the output should be centered.

There is also a great utility to format long numbers to make them more readable:

n = 808017424794512875886459904961710757005754368000000000
print(f'The number is: {n:_d}') # The number is: 808_017_424_794_512_875_886_459_904_961_710_757_005_754_368_000_000_000
print(f'The number is: {n:,d}') # The number is: 808,017,424,794,512,875,886,459,904,961,710,757,005,754,368,000,000,000

Pro tip:

Did you know that you can define numeric values in Python and format them with an underscore _ to make large numbers more readable?

million = 1_000_000
billion = 1_000_000_000
print(million, billion) # Will print 1000000 1000000000
print(f'{million:_d} {billion:_d}') # Will print 1_000_000 1_000_000_000

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

No responses yet