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
There are various approaches using which you can do string formatting in Python.
- String formatting using % operator
- Formatting using str.format() method
- String interpolation using f-strings
- 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:
- Floating point number to fixed point conversion in Python
- Single digit number to double digits string conversion in Python
- String Immutability with Examples in Python
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Programs to Print Patterns – Pyramid, Triangle using Star
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- String Slicing in Python with Examples
- Lists in Python with Examples
- Find difference between two given numbers in Python
- String indexing in Python with Examples
- Embed HTML within IPython Notebook
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- TypeError: a bytes-like object is required, not ‘str’ – Python3
- Extract numbers from a string in python
- Java String substring() example program
References