String Formatting

  What is string formatting in Python?


String formatting lets you inject items into a string rather than trying to chain items together using commas or string concatenation.

There are three ways to perform string formatting : -

The oldest method involves placeholders using the modulo % character.

An improved technique uses the .format() string method.

The newest method introduced with Python 3.6, uses formatted string literals,

 called f-strings.let's see one by one

Formatting with placeholders :

You can use %s to inject strings into your print statements.

Modulo % is referred to as a "string formatting operator".

You can pass multiple items by placing them inside a tuple after the % operator.

You can also pass variable names let's have practical examples for better understandings


Format conversion methods:

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 later on in the course, but you should note that %r and repr() deliver the string representation of the object, including quotation marks and any escape 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 let's have practical examples for better understandings


Padding and Precision of Floating Point Numbers:

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 how many numbers to show past the decimal point, let's have practical examples for better understandings.


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')

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

Inserted objects can be called by index position

Inserted objects can be assigned keywords 

Inserted objects can be reused, avoiding duplication let's have practical examples for better understandings


Formatted String Literals (f-strings):

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).let's have practical examples for better understandings



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}} 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: If this becomes important, you can always use .format() method syntax inside an f-string: let's have practical examples for better understandings

for more details, you simply check out the documentation https://www.python.org/doc/ and for any query just comment in the box so that I solve your queries as soon as possible.

Comments