Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Advanced Formatting: Positioning | Strings Formatting
String Manipulation in Python
course content

Cursusinhoud

String Manipulation in Python

String Manipulation in Python

1. Basic Concepts
2. String Methods
3. Strings Formatting

book
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:

OptionMeaningComments
<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,

1
print(".{:-^10}.".format('test'))
copy

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

Taak

Swipe to start coding

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 6
toggle bottom row

book
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:

OptionMeaningComments
<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,

1
print(".{:-^10}.".format('test'))
copy

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

Taak

Swipe to start coding

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 6
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt