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

Зміст курсу

String Manipulation in Python

String Manipulation in Python

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

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

Завдання

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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 6
toggle bottom row

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

Завдання

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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 6
toggle bottom row

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

Завдання

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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

Завдання

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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 6
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt