Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Positional Formatting | 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

bookPositional Formatting

Instead of printing a message with some variables inserted, we can save string as the pattern with {}, and then insert variables when needed.

To execute this, we use the same approach as for f-strings. For example, we can save a pattern as a variable. Let it be the following:

Having such string saved, we can now use .format method to fill the {} places. If we leave these places empty, then the positional approach will be applied.

To fill the {} places we use .format() method with all the variables we want to insert from left to right. The number of {} blocks and variables/values within .format function should be the same. In our example, it should be as follows:

123
pattern = "The price of the {} is {} $" print(pattern.format("laptop", 1000)) print(pattern.format("sofa", 300))
copy

Завдання

Create string named greeting in the format "Glad to see you, name on this wonderful day!". name and day are variables, already created for you. Then, print this string with the name and day inserted in the correct order.

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

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

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

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

bookPositional Formatting

Instead of printing a message with some variables inserted, we can save string as the pattern with {}, and then insert variables when needed.

To execute this, we use the same approach as for f-strings. For example, we can save a pattern as a variable. Let it be the following:

Having such string saved, we can now use .format method to fill the {} places. If we leave these places empty, then the positional approach will be applied.

To fill the {} places we use .format() method with all the variables we want to insert from left to right. The number of {} blocks and variables/values within .format function should be the same. In our example, it should be as follows:

123
pattern = "The price of the {} is {} $" print(pattern.format("laptop", 1000)) print(pattern.format("sofa", 300))
copy

Завдання

Create string named greeting in the format "Glad to see you, name on this wonderful day!". name and day are variables, already created for you. Then, print this string with the name and day inserted in the correct order.

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

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

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

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

bookPositional Formatting

Instead of printing a message with some variables inserted, we can save string as the pattern with {}, and then insert variables when needed.

To execute this, we use the same approach as for f-strings. For example, we can save a pattern as a variable. Let it be the following:

Having such string saved, we can now use .format method to fill the {} places. If we leave these places empty, then the positional approach will be applied.

To fill the {} places we use .format() method with all the variables we want to insert from left to right. The number of {} blocks and variables/values within .format function should be the same. In our example, it should be as follows:

123
pattern = "The price of the {} is {} $" print(pattern.format("laptop", 1000)) print(pattern.format("sofa", 300))
copy

Завдання

Create string named greeting in the format "Glad to see you, name on this wonderful day!". name and day are variables, already created for you. Then, print this string with the name and day inserted in the correct order.

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

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

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

Instead of printing a message with some variables inserted, we can save string as the pattern with {}, and then insert variables when needed.

To execute this, we use the same approach as for f-strings. For example, we can save a pattern as a variable. Let it be the following:

Having such string saved, we can now use .format method to fill the {} places. If we leave these places empty, then the positional approach will be applied.

To fill the {} places we use .format() method with all the variables we want to insert from left to right. The number of {} blocks and variables/values within .format function should be the same. In our example, it should be as follows:

123
pattern = "The price of the {} is {} $" print(pattern.format("laptop", 1000)) print(pattern.format("sofa", 300))
copy

Завдання

Create string named greeting in the format "Glad to see you, name on this wonderful day!". name and day are variables, already created for you. Then, print this string with the name and day inserted in the correct order.

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