String Concatenation
Concepts covered: Strings
The + operator joins 2 strings end to end and gives back a new string containing both. Nothing is inserted between them: if you want a space, you concatenate a space. Swapping the operands changes the result, so "Ada" + "Lovelace" is a different string from "Lovelace" + "Ada".
first = "Ada"
last = "Lovelace"
print(first + last)
print(first + " " + last)
print(last + ", " + first)Output:
AdaLovelace Ada Lovelace Lovelace, Ada
Each of the 3 expressions produces a new string. first and last are unchanged afterwards. + reads the strings it is given and produces a fresh result, which you then print or store in a variable. The strings you started with are never modified.
Growing a string in a loop
Because + returns a new string, building text piece by piece means assigning the result back to the same variable on each pass. The animation walks the digits of a date code and attaches each one to label with a dash in front of it, while day keeps its original value throughout.
day = "0301"
label = "d"
for ch in day:
label = label + "-" + ch
print(label)
print(day)d-0 d-0-3 d-0-3-0 d-0-3-0-1 0301
Each pass replaces label with a longer string. It starts as d-0 and grows through d-0-3 and d-0-3-0 until it reaches d-0-3-0-1. The last line prints 0301, confirming that reading characters from day left it as it was. This assign-back pattern, label = label + something, is how you build a CSV row from its fields, and report lines and log messages get assembled the same way.
Repeating with *
Multiplying a string by an integer repeats it that many times, which is the quickest way to draw a separator or pad a column. "-" * 10 is 10 dashes, and "ab" * 3 is ababab. Multiplying by 0 gives the empty string. As with numbers, * runs before +, so "=" * 3 + "end" repeats first and joins second.
line = "-" * 12
print(line)
print("Total: 40")
print(line)
print("=" * 3 + "end" + "=" * 3)Output:
------------ Total: 40 ------------ ===end===
Only strings join strings
+ between a string and a number stops the program. Python won't guess whether you wanted Order 42 or arithmetic, so it raises a TypeError and the lines after it never run. The error message names both types, which is how you find the mistake in a longer program.
order = 42
print("Order " + order)
print("never reached")You already have a tool that prints mixed values side by side: print with commas. It accepts strings and numbers alike and puts 1 space between each pair. Use + when you need the joined text as a value you can store or return, and commas when all you need is to display it.
order = 42
print("Order", order, "shipped")
print("Order " + "42" + " shipped")Output:
Order 42 shipped Order 42 shipped
- + between strings
- Produces a value you can store or return
- Adds nothing between the pieces
- Every piece must be a string
- Commas inside print
- Displays the values and produces nothing
- Puts 1 space between the pieces
- Accepts strings mixed with ints or floats
What does this program print?
a = "3"
b = "4"
print(a + b)
print(a * 2 + b)- 34, then 334
- 7, then 10
- 34, then 64
- 34, then 3 3 4
A count in a variable sets the repeats
The number on the right of * can come from a variable, which lets a count decide how long a piece of text is. Here a string and an int meet in one expression without an error: * accepts an int and repeats the text, while + refuses one. A string on both sides of * fails the other way, with TypeError: can't multiply sequence by non-int of type 'str'.
done = 3
left = 2
bar = "#" * done + "." * left
print("[" + bar + "]")
print("=" * done)Output:
[###..] ===
Both pieces of bar are computed first, because * runs before +, and then the brackets are concatenated around the result. Change done to 4 and left to 1 and the bar redraws as [####.] with no other edit.
About this lesson
This section is part of the Strings: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.