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

Course Content

String Manipulation in Python

String Manipulation in Python

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

bookFormatting by Keys

As for now, we can format strings in a specific order. It can be useful if we know in what order the values/variables will be given. But what to do if we have no idea about the order? Dictionaries can help us to avoid this issue.

As we may recall, a dictionary - is one of the data types in Python. Data in dictionary stores in pairs: key:value. This aspect will help us with formatting. For example, we know that we want to insert a person's name and age. We don't have to be worried about the order of the values. Instead of placing indices within the {} blocks, we will put some dummy variable (dictionary) with key (without quotes!). Then we will need to specify this variable as an argument for format() function. For example,

123
person = {'name': 'Alex', 'age': 23} pattern = "{d[name]} is {d[age]} years old" print(pattern.format(d = person))
copy

Please note that the keys we write within the dummy variable must be in your dictionary. We can't refer to {d[type]} not having a type key in the dictionary.

Task

Given dictionary geo with keys country and capital. You need to create the pattern geo_str "The capital of country is capital". country and capital - are values of dictionary geo by eponymous keys. Then, you need to print this string formatted by using dictionary geo.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
toggle bottom row

bookFormatting by Keys

As for now, we can format strings in a specific order. It can be useful if we know in what order the values/variables will be given. But what to do if we have no idea about the order? Dictionaries can help us to avoid this issue.

As we may recall, a dictionary - is one of the data types in Python. Data in dictionary stores in pairs: key:value. This aspect will help us with formatting. For example, we know that we want to insert a person's name and age. We don't have to be worried about the order of the values. Instead of placing indices within the {} blocks, we will put some dummy variable (dictionary) with key (without quotes!). Then we will need to specify this variable as an argument for format() function. For example,

123
person = {'name': 'Alex', 'age': 23} pattern = "{d[name]} is {d[age]} years old" print(pattern.format(d = person))
copy

Please note that the keys we write within the dummy variable must be in your dictionary. We can't refer to {d[type]} not having a type key in the dictionary.

Task

Given dictionary geo with keys country and capital. You need to create the pattern geo_str "The capital of country is capital". country and capital - are values of dictionary geo by eponymous keys. Then, you need to print this string formatted by using dictionary geo.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
toggle bottom row

bookFormatting by Keys

As for now, we can format strings in a specific order. It can be useful if we know in what order the values/variables will be given. But what to do if we have no idea about the order? Dictionaries can help us to avoid this issue.

As we may recall, a dictionary - is one of the data types in Python. Data in dictionary stores in pairs: key:value. This aspect will help us with formatting. For example, we know that we want to insert a person's name and age. We don't have to be worried about the order of the values. Instead of placing indices within the {} blocks, we will put some dummy variable (dictionary) with key (without quotes!). Then we will need to specify this variable as an argument for format() function. For example,

123
person = {'name': 'Alex', 'age': 23} pattern = "{d[name]} is {d[age]} years old" print(pattern.format(d = person))
copy

Please note that the keys we write within the dummy variable must be in your dictionary. We can't refer to {d[type]} not having a type key in the dictionary.

Task

Given dictionary geo with keys country and capital. You need to create the pattern geo_str "The capital of country is capital". country and capital - are values of dictionary geo by eponymous keys. Then, you need to print this string formatted by using dictionary geo.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

As for now, we can format strings in a specific order. It can be useful if we know in what order the values/variables will be given. But what to do if we have no idea about the order? Dictionaries can help us to avoid this issue.

As we may recall, a dictionary - is one of the data types in Python. Data in dictionary stores in pairs: key:value. This aspect will help us with formatting. For example, we know that we want to insert a person's name and age. We don't have to be worried about the order of the values. Instead of placing indices within the {} blocks, we will put some dummy variable (dictionary) with key (without quotes!). Then we will need to specify this variable as an argument for format() function. For example,

123
person = {'name': 'Alex', 'age': 23} pattern = "{d[name]} is {d[age]} years old" print(pattern.format(d = person))
copy

Please note that the keys we write within the dummy variable must be in your dictionary. We can't refer to {d[type]} not having a type key in the dictionary.

Task

Given dictionary geo with keys country and capital. You need to create the pattern geo_str "The capital of country is capital". country and capital - are values of dictionary geo by eponymous keys. Then, you need to print this string formatted by using dictionary geo.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt