Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Advanced Formatting: Positioning
course content

Course Content

String Manipulation in Python

Advanced Formatting: PositioningAdvanced Formatting: Positioning

Nicely done! Sometimes we may want to transform our string a bit so the text will be centered, the number will be displayed with respective sign (+ or -) or the number will be rounded with specific precision. Let's consider aligning at first.

To do it, we need to set formatting type preceded by colon within curly brackets. Let's consider them:

<Forces the field to be left-aligned within available spaceDefault method for most objects
>Forces the field to be right-aligned within available spaceDefault method for a number
=Forces the padding to be placed after the sign to the leftmost positionUseful for strings like 00000132
^Forces the field to be centered within the available spaceno special uses

Using codes with no additions is not the best practice since there may be no differences after formatting. Let's consider additional arguments. We can place the symbol (only one!) we want to fill free space between colon : and argument from the table above. Then, we can also add the length you want to align your string in. For example,

As we can see, we set the symbol - to fill, and the string length to 10 (and there were 3 - symbols on both sides of test, since 3 + 4 (length of test) + 3 = 10).


Within .format() function set the correct format to do the next: add 0 on the left side to '8923'. The substring with zeros and number should be of length 12.

Everything was clear?

Section 3. Chapter 6
toggle bottom row