11---
2- description : String concatenation and interpolation in python
2+ description : String concatenation and interpolation in Python
33---
44
55# String Interpolation and Concatenation
@@ -25,9 +25,9 @@ Enter the second number: 12
2525The sum of 23 and 12 is 2312
2626```
2727
28- We briefly mentioned that this was caused by _ string concatenation_ . In this chapter, we will look
29- at what concatenation is, and at a more powerful way of combining text and variables called
30- _ string interpolation_ .
28+ We briefly touched on the fact that this was caused by _ string concatenation_ . In this chapter, we
29+ will look at what concatenation is, and at a more powerful way of combining text and variables
30+ called _ string interpolation_ .
3131
3232## Why do we need this?
3333
@@ -42,13 +42,13 @@ age = 34
4242print (" My name is" , name, " and I am" , age, " years old." )
4343```
4444
45- This works, but it only works with ` print ` . What if we want to store the full sentence in a variable
46- instead? This is where concatenation and interpolation come in.
45+ This works, but it only works with ` print ` . What if we want to, for example, store the full sentence
46+ in a variable instead? This is where concatenation and interpolation come in.
4747
4848## String Concatenation
4949
5050Concatenation simply means joining two or more strings together, one after the other. In Python, we
51- do this with the ` + ` operator.
51+ can do this with the ` + ` operator.
5252
5353``` python
5454first_name = " Alan"
@@ -64,12 +64,12 @@ Alan Turing
6464
6565Notice that we added ` " " ` (a string containing a single space) in the middle. Concatenation joins
6666strings _ exactly_ as they are, so Python will not add any spaces for us. Without it, we would get
67- ` AlanTuring ` .
67+ ` AlanTuring ` as the output .
6868
6969### Concatenating with other data types
7070
71- Concatenation only works between strings. If we try to join a string with a number, Python will
72- raise an error:
71+ Concatenation only works between strings. This means that if we try to join a string with a number,
72+ Python will raise an error:
7373
7474``` python
7575name = " Alan"
@@ -97,11 +97,6 @@ print("My name is " + name + " and I am " + str(age) + " years old.")
9797My name is Alan and I am 34 years old.
9898```
9999
100- !!! note
101-
102- Keep in mind that we can only add strings to strings and numbers to numbers. You cannot add strings
103- to numbers without first type casting them.
104-
105100## String Interpolation
106101
107102Interpolation is the process of inserting values directly into a string. Instead of breaking the
@@ -125,7 +120,7 @@ My name is Alan and I am 34 years old.
125120```
126121
127122That's the same output as before, but much easier to read and write! Also notice that we didn't need
128- to use ` str(age) ` . An f-string converts the values for us automatically.
123+ to use ` str(age) ` . This is because f-strings convert the values for us automatically.
129124
130125!!! warning
131126
@@ -134,8 +129,8 @@ to use `str(age)`. An f-string converts the values for us automatically.
134129
135130#### Expressions inside f-strings
136131
137- The curly braces don't only accept variable names. You can put any valid Python expression inside
138- them, and Python will work out the result:
132+ You can put any valid Python expression (not just variable names) inside the curly braces, and
133+ Python will work out the result:
139134
140135``` python
141136price = 20
@@ -151,8 +146,8 @@ Total cost: 60
151146#### Formatting values
152147
153148You can also control how a value is displayed by adding a colon ` : ` followed by a format
154- specification inside the braces. One of the most common uses is limiting the number of decimal
155- places of a float:
149+ specification inside the braces. One of the most common uses of this is limiting the number of
150+ decimal places of a float:
156151
157152``` python
158153pi = 3.14159265
@@ -168,12 +163,11 @@ Here, `.2f` means "show this as a float with 2 digits after the decimal point".
168163
169164### The ` str.format() ` method
170165
171- Before f-strings were introduced in Python 3.6, the most common way to interpolate was the
172- ` format() ` method of strings. You will still see this in a lot of older code, so it is worth
173- knowing.
166+ Before f-strings were introduced in Python 3.6, the most common way to interpolate was the ` format `
167+ method of strings. You will still see this in a lot of older code, so it is worth knowing.
174168
175169Instead of placing the variables inside the string, you leave empty curly braces ` {} ` as
176- placeholders, and pass the values to ` .format() ` afterwards. The values are filled in order:
170+ placeholders, and pass the values to ` .format() ` afterwards. The values are filled in order like so :
177171
178172``` python
179173name = " Alan"
@@ -198,8 +192,10 @@ print("{n} is {a} years old.".format(n=name, a=age))
198192
199193!!! note
200194
201- Anything after a `#` on a line is a comment. Python ignores it completely. Comments are notes for
202- humans reading the code.
195+ Anything after a `#` on a line is a comment. Comments are notes for humans reading the code, so
196+ Python ignores them completely.
197+
198+ It's output:
203199
204200``` text
205201Alan is 34 years old.
@@ -208,8 +204,8 @@ Alan is 34 years old.
208204
209205## Which one should I use?
210206
211- All three approaches produce the same kind of result, a new string. Here's a quick comparison using
212- the same example:
207+ All three approaches produce the same kind of result: a new string. Here's a quick comparison using
208+ the same example.
213209
214210``` python
215211name = " Alan"
@@ -220,10 +216,9 @@ print("My name is {} and I am {} years old.".format(name, age)) # str.form
220216print (f " My name is { name} and I am { age} years old. " ) # f-string
221217```
222218
223- As a general rule:
219+ Here are some general rules for their usage.
224220
225- - Use ** f-strings** for most situations. They are the shortest, the most readable, and the most
226- common way to write modern Python.
227- - Use ** ` + ` concatenation** for simple cases where you want to join two variables containing strings
228- only.
221+ - Use ** f-string** interpolation for most situations. They are the shortest, the most readable, and
222+ the most common way to write modern Python.
223+ - Use ** concatenation (+)** for simple cases where you want to join two strings only.
229224- ** ` str.format() ` ** when you are reading older code.
0 commit comments