Course Content
String Manipulation in Python
String Manipulation in Python
Advanced 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:
Option | Meaning | Comments |
< | Forces the field to be left-aligned within available space | Default method for most objects |
> | Forces the field to be right-aligned within available space | Default method for a number |
= | Forces the padding to be placed after the sign to the leftmost position | Useful for strings like 00000132 |
^ | Forces the field to be centered within the available space | no 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,
print(".{:-^10}.".format('test'))
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).
Task
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.
Thanks for your feedback!
Advanced 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:
Option | Meaning | Comments |
< | Forces the field to be left-aligned within available space | Default method for most objects |
> | Forces the field to be right-aligned within available space | Default method for a number |
= | Forces the padding to be placed after the sign to the leftmost position | Useful for strings like 00000132 |
^ | Forces the field to be centered within the available space | no 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,
print(".{:-^10}.".format('test'))
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).
Task
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.
Thanks for your feedback!
Advanced 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:
Option | Meaning | Comments |
< | Forces the field to be left-aligned within available space | Default method for most objects |
> | Forces the field to be right-aligned within available space | Default method for a number |
= | Forces the padding to be placed after the sign to the leftmost position | Useful for strings like 00000132 |
^ | Forces the field to be centered within the available space | no 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,
print(".{:-^10}.".format('test'))
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).
Task
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.
Thanks for your feedback!
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:
Option | Meaning | Comments |
< | Forces the field to be left-aligned within available space | Default method for most objects |
> | Forces the field to be right-aligned within available space | Default method for a number |
= | Forces the padding to be placed after the sign to the leftmost position | Useful for strings like 00000132 |
^ | Forces the field to be centered within the available space | no 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,
print(".{:-^10}.".format('test'))
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).
Task
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.