String formatting in python with examples

Python String Formatting – Examples

In this tutorial you will learn about Python String Formatting best practices with examples. You will learn four approaches to format the strings in Python.

Python String Formatting – Examples

String formatting in python with examples


There are various approaches using which you can do string formatting in Python.

  1. String formatting using % operator
  2. Formatting using str.format() method
  3. String interpolation using f-strings
  4. Template Strings – Python library

String Formatting using % Operator

Strings in Python supports built-in operation to enable simple formatting using % operator. And this is old style of formatting strings.

For example,

>>> name = "John"
>>> 'Hello, %s' %name
'Hello, John'

In the above example, % format specifier tells Python to substitute the value of name replacing %s.

Formatting using str.format() method

This is the best way and also new way to do string formatting. In this method you don’t have to use % operator and all you just have to do is to call .format() method on the string object as shown below.

>>> 'hello world! {}'.format('welcome')
'hello world! welcome'

Note, this method is best suited to format strings in print statements as shown below.

>>> print('hello world! {}'.format('welcome'))
hello world! welcome

Some more examples for print formatting with strings:

>>> print('I am {} {} {}'.format('very', 'good', 'man'))
I am very good man

In the above print statement, the .format() is going to insert the strings for curly braces in the same order it is specified.

If you wanted to change the order then you can specify the required string indices inside the curly braces as shown below.

>>> print('I am {2} {0} {1}'.format('healthy', 'boy', 'a'))
I am a healthy boy

String Interpolation or f-strings

From Python 3.6 a new string formatting approach was introduced called formatted string literals or f-strings. In this approach you can embed Python expressions inside the string constants.

For example,

>>> name = "John"
>>> f'Hello, {name}!'
'Hello, John!'

Also, you can see from the above example (second line), we are using prefix before the string constant with letter “f“. Hence this approach is called as f-strings.

This new string formatting approach is very powerful and helpful since you can embed Python expressions, so that you can do inline arithmetic operations as shown below.

>>> x = 2

>>> y = 5

>>> f'When 2 is multiplied by 5 the result is {x*y}.'
'When 2 is multiplied by 5 the result is 10.'

Template Strings Library

Template strings is the another option to do string formatting in Python. But this option is not widely used or less powerful compared to str.format() and f-strings methods.

For example,

>>> name ="John"

>>> from string import Template
... t = Template('Hello!, $name')
... t.substitute(name=name)

'Hello!, John'

That’s it. Basically, you had learnt various approaches to format strings in Python.

Hope it helped 🙂

You’ll also like:



Inline Feedbacks
View all comments