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

Kursinnhold

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

Oppgave

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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 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).

Oppgave

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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 6
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt