Wednesday, October 21, 2015

Python Tutorial: Advanced String Formatting In Python - Python Strings #77



Advanced String Formatting In Python

Advanced String Formatting In Python

In this Python tutorial, we will dig deeper into the Python formatting string method. We can perform a lot of tasks using the Python formatting string method in this tutorial, we will cover some of the most used features of this method but we cannot cover them all. If you would like to see the full list of features visits the Python documentation.
In the previous tutorial, we went over the basics of the Python formatting string method which gave us a good understanding of the power of string formatting in Python. When using formatting we can modify existing strings to help our programs to be more flexible and allows us to adapt our strings to fit certain situations.

Positions Content Using String Formatting in Python

We can use string formatting to position our content within a string just like the ljust string method, center string method and rjust string method. Let's Take a look at this in action.

Left Justify String Formatting Method

>>> '{:<40}'.format('Left')
'Left                                    '
Left Justify String Formatting Method Explained
  • '{:<40}'.format('Left') - In this example we add our placeholder({}) inside the curly brackets we insert colon(:) which is part of the string formatting syntax and then we add a carrot(<) pointing to the left which indicates we want to left justify the content. We also include 40 which is how many index spaces we want our new string object to span. The we call the format string method on our string object and provide a string as the argument.
  • 'Left                                    ' - We are returned a new string object that contains our argument and it is left justified with the string object expanding forty index positions.

Right Justify String Formatting Method

>>> '{:>40}'.format('right')
'                                   right'
  • '{:>40}'.format('right') We create a new string object that contains our placeholder({}) inside the placeholder we place a colon(:) which is part of string formatting syntax and we place a carrot(>) which is pointing to the right which indicates we want to right justify the content. We include 40 which states we want the new line object to span forty index positions.
  • '                                   right' - We are returned a new string object that contains our argument which is right justified within 40 index spaces.

Center Justify String Formatting Method

>>> '{:^40}'.format('center')
'                 center                 '
  • '{:^40}'.format('center') - We create a new string object which contains a placeholder({}) and this placeholder contains a colon which is part of the syntax, an up carrot(^) which indicates we want to center our content in the string object. We indicate we want our string to span forty index positions.
  • '                 center                 - We are returned a new string object which contains our argument which is centered within our forty index positions.

Binary String Formatting Method

If we would like to display the binary number of a value we can use the binary string format.

How To Get The Binary Number

>>> '{:b}'.format(12331)
'11000000101011'
  • '{:b}'.format(12331) We establish a new string object in which we will use to get the binary number of the value. We place our placeholder({}) in the string object. Inside our placeholder, we place the colon(:) which is part of the syntax then we insert a b which will indicate we want the binary representation of our value. Then we call format string method on our string object with the argument of 12331 and this is value we want to know the binary representation of our value.
  • '11000000101011' - We are our returned a new string object that contains our binary representation of the value 12331.

How To Get The Prefix For A Binary Number

>>> '{:#b}'.format(12331)
'0b11000000101011'
  • '{:#b}'.format(12331) - In the above example we get the binary number of our value in this example we include a # sign to indicate we want the prefix for a binary number. The rest remains the same.
  • '0b11000000101011' - We are returned a new string object that contains the binary representation of 12331 but we are also returned the the prefix for binary format which is 'Ob'.

Thousands Separator Via The Formatting String Method

>>> '{:,}'.format(21324232342342325)
'21,324,232,342,342,325'
  • '{:,}'.format(21324232342342325) - In this example, we want to separate our number into thousands to easy read the number. We achieve this by creating a new string object and we place our placeholder({}) in our string object. Then we place a colon(:) in our string object and then place a comma(,) in our placeholder. The comma indicates to Python that we want to comma separate our number. We then call the format string method on our string object and provide our value as an argument.
  • '21,324,232,342,342,325' - We are returned a string object that contains thousand separated string. Notice that this will place a comma in after every third number from the right.

Percentages In The Formatting String Method

>>> correct = 35
>>> total = 40
>>> 'Your Score Is: {:.1%}'.format(correct/total)
'Your Score Is: 87.5%'
  • correct = 35 - We create a new number object that is represented by the name correct.
  • total = 40 - We create a new number object that is represented by the name total.
  • 'Your Score Is: {:.1%}'.format(correct/total) - We establish a new string object that contains some content. We then place a placeholder({}) inside the placeholder we place a colon(:) which is a part of the syntax. Then we place a period(.) which indicates we want a decimal number returned. The number following the period indicates how many numbers after the decimal we would like to display. We then add the percentage(%) symbol to be contained in the returned string object. Then we call the format string method on our string object. We provide two arguments which are divided by each other.
  • 'Your Score Is: 87.5%' - We are returned a new string object that takes our arguments that have been divided by each other. 
     

Conclusion

In this Python tutorial we have looked at advanced string formatting string method. If you have any questions please leave a comment below.

No comments:

Post a Comment