Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
be4f92b
Initialized level 01 - chapter 05
Jiggly-Balls Aug 31, 2026
9f73de8
Initialized string concatenation & interpolation document
Jiggly-Balls Sep 5, 2026
6656c3c
Initialized string concatenation & interpolation document
Jiggly-Balls Sep 5, 2026
4ca948f
Finished chapter 5
Jiggly-Balls Oct 9, 2026
7da0b1e
Merge branch 'feature/python-tutorial' into feature/tutorial-lvl01-ch05
Jiggly-Balls Oct 9, 2026
4a1055b
Fixed chapter ordering & formatting
Jiggly-Balls Oct 10, 2026
f898cdf
Fix grammatical errors
Jiggly-Balls Oct 10, 2026
340ba16
Merge pull request #14 from practical-python-org/feature/tutorial-lvl…
Jiggly-Balls Oct 10, 2026
1fb8a53
Merge branch 'main' into feature/python-tutorial
Jiggly-Balls Oct 10, 2026
4c4dfa1
Fix minor formatting
Jiggly-Balls Oct 11, 2026
d683e5c
Added chapter 06-operators-pt01
Jiggly-Balls Oct 11, 2026
b509605
Finished Arithmetic section
Jiggly-Balls Oct 11, 2026
3e25b78
Fix output errors, grammar & added comparison with strings
Jiggly-Balls Oct 11, 2026
dc9ea11
Rewording
Jiggly-Balls Oct 11, 2026
b396517
Fixes for Maha reviews
Jiggly-Balls Oct 13, 2026
39783eb
Merge pull request #18 from practical-python-org/feature/tutorial-lvl…
Jiggly-Balls Oct 13, 2026
80e662a
Remove comments
Jiggly-Balls Oct 14, 2026
778607e
Added chapter 7 - finished introduction & if statement
Jiggly-Balls Oct 18, 2026
2c35714
Added indentation & condional statement animation
Jiggly-Balls Oct 18, 2026
be3ec2f
Updated background colour of images in Python tutorial
Jiggly-Balls Oct 18, 2026
20c9ae4
Merge pull request #19 from practical-python-org/fix/tutorial-images
Jiggly-Balls Oct 18, 2026
8547308
Added conditional statement animations
Jiggly-Balls Oct 23, 2026
de34881
Finished chapter 7 - conditional statemnets
Jiggly-Balls Oct 23, 2026
10caaae
Fix errors
Jiggly-Balls Oct 27, 2026
51d8499
Added truthy & falsy values explaination
Jiggly-Balls Oct 27, 2026
ba66a25
Fixed the code in animation
Jiggly-Balls Oct 29, 2026
6f6113a
Merge pull request #20 from practical-python-org/feature/tutorial-lvl…
Jiggly-Balls Oct 29, 2026
0d03a93
Merge branch 'main' into feature/python-tutorial
Jiggly-Balls Oct 29, 2026
120d984
Missed a comma
Jiggly-Balls Oct 29, 2026
0a9f603
Merge branch 'feature/tutorial-lvl01-ch07' into feature/python-tutorial
Jiggly-Balls Oct 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions docs/python-tutorial/chapters/02-intro-to-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,17 @@ Python. There are 4 simple rules which follow:
Here is a list of **invalid** python variables:

```python
full name = "James Bond" # Contains a space
age! = 90 # Contains a special character
finally = "yes" # Conflicts with the Python keyword "finally"
20money = 900 # Starts with a number
full name = "James Bond"
age! = 90
finally = "yes"
20money = 900
```

1. `full name` contains a space in it
1. `age!` contains a special character (`!`)
1. `finally` conflicts with the Python keyword of the same name
1. `20money` starts with a number

Below is a list of **valid** python variables:

```python
Expand Down
224 changes: 224 additions & 0 deletions docs/python-tutorial/chapters/05-string-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
---
description: String concatenation and interpolation in Python
---

# String Interpolation and Concatenation

We learned what the string data type was from our [third chapter](03-data-types-pt01.md). A quick
recap: Strings are used to represent any textual data.

We also saw how adding two inputs resulted in a different output than we expected.

```py
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
result = num1 + num2

print("The sum of", num1, "and", num2, "is", result)
```

After running the program:

```text
Enter the first number: 23
Enter the second number: 12
The sum of 23 and 12 is 2312
```

We briefly touched on the fact that this was caused by _string concatenation_. In this chapter, we
will look at what concatenation is, and at a more powerful way of combining text and variables
called _string interpolation_.

## Why do we need this?

Most programs need to build sentences out of a mix of fixed text and changing data. For example,
greeting a user by name, or showing a player's score. So far, the only way we've done this is by
passing multiple values to `print`:

```python
name = "Alan"
age = 34

print("My name is", name, "and I am", age, "years old.")
```

This works, but it only works with `print`. What if we want to, for example, store the full sentence
in a variable instead? This is where concatenation and interpolation come in.

## String Concatenation

Concatenation simply means joining two or more strings together, one after the other. In Python, we
can do this with the `+` operator.

```python
first_name = "Alan"
last_name = "Turing"

full_name = first_name + " " + last_name
print(full_name)
```

```text
Alan Turing
```

Notice that we added `" "` (a string containing a single space) in the middle. Concatenation joins
strings _exactly_ as they are, so Python will not add any spaces for us. Without it, we would get
`AlanTuring` as the output.

### Concatenating with other data types

Concatenation only works between strings. This means that if we try to join a string with a number,
Python will raise an error:

```python
name = "Alan"
age = 34

print("My name is " + name + " and I am " + age + " years old.")
```

```text
TypeError: can only concatenate str (not "int") to str
```

Python doesn't know whether `"I am " + 34` should mean "add" or "join", so instead of guessing, it
stops with an error. To fix it, we need to convert the number to a string first. Remember type
casting from the previous chapter? We can use the `str` function for this:

```python
name = "Alan"
age = 34

print("My name is " + name + " and I am " + str(age) + " years old.")
```

```text
My name is Alan and I am 34 years old.
```

## String Interpolation

Interpolation is the process of inserting values directly into a string. Instead of breaking the
string apart and gluing the pieces back together, we write the whole sentence at once and mark the
places where the values should go. Python gives us two ways of doing this.

### f-strings

An f-string (short for _formatted string literal_) is a string with an `f` placed right before the
opening quote. Inside it, anything wrapped in curly braces `{}` is replaced with its value.

```python
name = "Alan"
age = 34

print(f"My name is {name} and I am {age} years old.")
```

```text
My name is Alan and I am 34 years old.
```

That's the same output as before, but much easier to read and write! Also notice that we didn't need
to use `str(age)`. This is because f-strings convert the values for us automatically.

!!! warning

Don't forget the `f` before the quote! Without it, Python treats the string as normal text and
prints the curly braces as-is: `My name is {name} and I am {age} years old.`

#### Expressions inside f-strings

You can put any valid Python expression (not just variable names) inside the curly braces, and
Python will work out the result:

```python
price = 20
quantity = 3

print(f"Total cost: {price * quantity}")
```

```text
Total cost: 60
```

#### Formatting values

You can also control how a value is displayed by adding a colon `:` followed by a format
specification inside the braces. One of the most common uses of this is limiting the number of
decimal places of a float:

```python
pi = 3.14159265

print(f"Pi is roughly {pi:.2f}")
```

```text
Pi is roughly 3.14
```

Here, `.2f` means "show this as a float with 2 digits after the decimal point".

### The `str.format()` method

Before f-strings were introduced in Python 3.6, the most common way to interpolate was the `format`
method of strings. You will still see this in a lot of older code, so it is worth knowing.

Instead of placing the variables inside the string, you leave empty curly braces `{}` as
placeholders, and pass the values to `.format()` afterwards. The values are filled in order like so:

```python
name = "Alan"
age = 34

print("My name is {} and I am {} years old.".format(name, age))
```

```text
My name is Alan and I am 34 years old.
```

You can also number the placeholders to control which value goes where, or give them names:

```python
# Using positions
print("{1} is {0} years old.".format(age, name))

# Using names
print("{n} is {a} years old.".format(n=name, a=age))
```

!!! note

Anything after a `#` on a line is a comment. Comments are notes for humans reading the code, so
Python ignores them completely.

It's output:

```text
Alan is 34 years old.
Alan is 34 years old.
```

## Which one should I use?

All three approaches produce the same kind of result: a new string. Here's a quick comparison using
the same example.

```python
name = "Alan"
age = 34

print("My name is " + name + " and I am " + str(age) + " years old.") # concatenation
print("My name is {} and I am {} years old.".format(name, age)) # str.format()
print(f"My name is {name} and I am {age} years old.") # f-string
```

Here are some general rules for their usage.

- Use **f-string** interpolation for most situations. They are the shortest, the most readable, and
the most common way to write modern Python.
- Use **concatenation (`+`)** for simple cases where you want to join two strings only.
- **`str.format()`** when you are reading older code.
Loading
Loading