String Formatting in Python

Techniques for Dynamic String Generation and Data Presentation

String formatting is a technique used to combine an existing string with other values to create a new string. This process is important in many programming languages, including Python, as it enables developers to create dynamic strings that can change based on different inputs. In data science, string formatting is particularly useful as it allows data scientists to present results in a clear and readable format, streamline their workflows, and effectively communicate their findings. With string formatting, data scientists can process, analyze, and visualize large amounts of data with ease. In this article, we will provide a comprehensive overview of string formatting in Python and explore the various methods available to help you get started with this important technique.

There are three ways to perform string formatting.

  • Placeholder Method using % character
  • .format() Method
  • f-strings Method

Formatting with placeholders

In Python, there are several placeholders that can be utilized for string formatting purposes. These placeholders include %s, %r, and %d. lets’ look some example of each placeholder:

print("I'm going to insert %s into the code." %'new text')
I'm going to insert new text into the code.

In this example, The string “I’m going to insert %s into the code.” serves as a template and includes a placeholder, represented by the %s. This placeholder indicates where a value should be inserted into the string.

The value that is inserted into the string is ’new text’, which is specified after the % operator. When the code is executed, the output is the string “I’m going to insert new text into the code.”, which shows how the value of ’new text’ has been incorporated into the template string.

print("I'm going to insert %s information here, and %s data here." %('critical','additional'))
I'm going to insert critical information here, and additional data here.

In this example, the placeholders %s are used to dynamically insert the values ‘critical’ and ‘additional’ into the string. The % operator is followed by a tuple of values that are inserted into the corresponding placeholders in the string.

a, b = 'important', 'updated'
print("I'm going to insert %s information here, and %s data here." %(a,b))
I'm going to insert important information here, and updated data here.

here, the variables a and b are assigned the values ‘important’ and ‘updated’ respectively. These variables are then used in the string formatting expression to insert the values into the corresponding placeholders in the string. This demonstrates that the values assigned to the variables can be changed easily, making the output dynamic and flexible.

It should be noted that two methods %s and %r convert any python object to a string using two separate methods: str() and repr(). We will learn more about these functions on upcomming posts, but you should note that %r and repr() deliver the string representation of the object, including quotation marks and any escape characters. for example:

print('My name is  %s.' %'Mohan')
print('My name is  %r.' %'Mohan')
My name is  Mohan.
My name is  'Mohan'.

In this example, the string ‘Mohan’ is being inserted into the sentence ‘My name is %s.’ and ‘My name is %r.’ using string formatting.

The %s placeholder is a placeholder for a string, and the % symbol followed by ‘Mohan’ in parentheses is used to replace the %s placeholder with the string ‘Mohan’. similarly, The %r placeholder is a placeholder for a string representation of an object, which can be useful for debugging purposes. In this case, the string representation of the string ‘Mohan’ is ‘Mohan’, so the %r placeholder is replaced with ‘Mohan’. let’s look another example of %r and %s

words = 'catch\nand\nrelease'

print('Remember to %s.' % ('catch\nand\nrelease'))
print('Remember to %r.' % ('catch\nand\nrelease'))
Remember to catch
and
release.
Remember to 'catch\nand\nrelease'.

here, first line’s result comes in three lines and the second line’s result comes in one line is due to the difference between %s and %r.

%s is used to insert a string representation of an object into a string, which means that any newline characters (\n) in the string representation will be displayed as separate lines in the output.

On the other hand, %r is used to insert a raw, string representation of an object into a string, which includes any escape sequences such as newline characters.

The %s operator converts whatever it sees into a string, including integers and floats. The %d operator converts numbers to integers first, without rounding. Note the difference below:

print('I wrote %s programs today.' %3.75)
print('I wrote %d programs today.' %3.75)   
I wrote 3.75 programs today.
I wrote 3 programs today.

Padding and Precision of Floating Point Numbers

In Python, you can control the display of floating-point numbers by using the % operator in combination with placeholder specifications. These placeholder specifications allow you to specify the number of decimal places to display, as well as the width of the output field. To control the precision of floating-point numbers, you can use the . character followed by the number of decimal places you want to display

print('Floating point numbers: %5.2f' %(13.144))

Floating point numbers: 13.14

Floating point numbers use the format %5.2f. Here, 5 would be the minimum number of characters the string should contain; these may be padded with whitespace if the entire number does not have this many digits. Next to this, .2f stands for two digits to show past the decimal point.

print('Floating point numbers: %.0f' %(1300.144))
Floating point numbers: 1300

Formatting with the .format() method

A better way to format objects into your strings for print statements is with the string .format() method. The syntax is:

'String here {} then also {}'.format('something1','something2')

For example:

print('This is a string with an {}'.format('insert'))
This is a string with an insert

The .format() method has several advantages over the %s placeholder method:

1. Inserted objects can be called by index position:
print('The {2} {1} {0}'.format('fox','brown','quick'))
The quick brown fox
2. Inserted objects can be assigned keywords:
print('First Object: {a}, Second Object: {b}, Third Object: {c}'.format(a=1,b='Two',c=12.3))
First Object: 1, Second Object: Two, Third Object: 12.3
3. Inserted objects can be reused, avoiding duplication:

print('I %s you more every day, and my %s for you will never fade.' %('love','love'))
#Vs
print('I {w} you more every day, and my {w} for you will never fade.'.format(w='love'))

I love you more every day, and my love for you will never fade.
I love you more every day, and my love for you will never fade.

Alignment, padding and precision with .format()

Within the curly braces you can assign field lengths, left/right alignments, rounding parameters and more

print('{0:8} | {1:9}'.format('Fruit', 'Quantity'))
print('{0:8} | {1:20}'.format('Apples', 3.))
print('{0:8} | {1:9}'.format('Oranges', 10))
Fruit    | Quantity 
Apples   |                  3.0
Oranges  |        10

By default, .format() aligns text to the left, numbers to the right. You can pass an optional <,^, or > to set a left, center or right alignment:

print('{0:<8} | {1:^8} | {2:>8}'.format('Left','Center','Right'))
print('{0:<8} | {1:^8} | {2:>8}'.format(11,22,33))
Left     |  Center  |    Right
11       |    22    |       33

You can precede the aligment operator with a padding character

print('{0:=<8} | {1:-^8} | {2:.>8}'.format('Left','Center','Right'))
print('{0:=<8} | {1:-^8} | {2:.>8}'.format(11,22,33))
Left==== | -Center- | ...Right
11====== | ---22--- | ......33

Field widths and float precision are handled in a way similar to placeholders. The following two print statements are equivalent:

print('This is my ten-character, two-decimal number:%10.2f' %13.579)
print('This is my ten-character, two-decimal number:{0:10.2f}'.format(13.579))
This is my ten-character, two-decimal number:     13.58
This is my ten-character, two-decimal number:     13.58

Note that there are 5 spaces following the colon, and 5 characters taken up by 13.58, for a total of ten characters.

Formatted String Literals (f-strings)

Introduced in Python 3.6, f-strings offer several benefits over the older .format() string method described above. For one, you can bring outside variables immediately into to the string rather than pass them as arguments through .format(var).

name = 'Fred'

print(f"He said his name is {name}.")
He said his name is Fred.

Pass !r to get the string representation:

print(f"He said his name is {name!r}")
He said his name is 'Fred'

Float formatting follows "result: {value:{width}.{precision}}"

Where with the .format() method you might see {value:10.4f}, with f-strings this can become {value:{10}.{6}}

num = 23.45678
print("My 10 character, four decimal number is:{0:10.4f}".format(num))
print(f"My 10 character, four decimal number is:{num:{10}.{3}}")
My 10 character, four decimal number is:   23.4568
My 10 character, four decimal number is:      23.5

Note that with f-strings, precision refers to the total number of digits, not just those following the decimal. This fits more closely with scientific notation and statistical analysis. Unfortunately, f-strings do not pad to the right of the decimal, even if precision allows it:

num = 23.45
print("My 10 character, four decimal number is:{0:10.4f}".format(num))
print(f"My 10 character, four decimal number is:{num:{10}.{6}}")
My 10 character, four decimal number is:   23.4500
My 10 character, four decimal number is:     23.45

If this becomes important, you can always use .format() method syntax inside an f-string:

num = 23.45
print("My 10 character, four decimal number is:{0:10.4f}".format(num))
print(f"My 10 character, four decimal number is:{num:10.4f}")
My 10 character, four decimal number is:   23.4500
My 10 character, four decimal number is:   23.4500
Mohan Kumar Pudasaini
Mohan Kumar Pudasaini
Data Analyst||Risk Analyst

Passionate about utilizing data to uncover valuable insights and drive actionable outcomes.

Related