1.2.10Introduction to Programming (Python)

String operations — concatenation, repetition

1,682 words8 min readdifficulty · medium

1. Concatenation with +

WHAT it does. "foot" + "ball""football". No space is inserted automatically — you only get exactly the characters you provide.

HOW it works (from first principles). Think of a string as a list of slots:

"abc"[a0, b1, c2]\text{"abc"} \equiv [\,a_0,\ b_1,\ c_2\,]

When you do s + t, Python builds a brand-new buffer of length len(s) + len(t), copies all of s's characters first, then all of t's characters after. The originals are untouched — strings are immutable, so an "operation" never edits in place; it returns a fresh string.


2. Repetition with *

WHY it exists. Writing "-" + "-" + "-" + ... for a 40-character line is painful. "-" * 40 says it once.

HOW it works. s * n is defined as repeated concatenation:

sn  =  s+s++sn copiess * n \;=\; \underbrace{s + s + \cdots + s}_{n \text{ copies}}

So it inherits all the concatenation rules.

Figure — String operations — concatenation, repetition

3. Combining both


Common mistakes


Flashcards

What operator concatenates two strings in Python?
+
What operator repeats a string n times?
* (with an integer)
What is len(s + t) in terms of len(s) and len(t)?
len(s) + len(t)
What is len(s * n)?
n * len(s) for n ≥ 0
What does "hi" * 0 return?
'' (empty string)
What does "hi" * -3 return?
'' (negative counts give empty string, no error)
Why does "3" + 4 fail?
You can't concatenate a str with an int; convert with str(4) first.
Why does "ab" * "2" fail?
Repetition needs an integer count, not a string; use int("2").
Are Python strings mutable or immutable?
Immutable — +/* return brand-new strings.
Does "a"+"b" insert a space?
No — only the exact characters you provide.

Recall Feynman: explain to a 12-year-old

Imagine letter beads on a string. Concatenation (+) is tying one bead-string onto the end of another to make a longer one — "cat" + "dog" becomes "catdog", nothing extra slipped in. Repetition (*) is a copy machine: "ha" * 3 prints "hahaha". You can't repeat something "minus two times," so Python just gives you an empty bead-string. And you can never add letter-beads to number-beads without first turning the number into letters — that's why str(4) exists.


Connections

Concept Map

has property

so ops

reuses +

reuses *

joins into

defined as repeated

length rule

length rule

evaluates

requires

requires

String = sequence of chars

Immutable

Operator overloading

+ concatenation

* repetition

Returns new string

len s+t = len s + len t

len s*n = n · len s

Left-associative

Type must match — use str

n non-negative integer

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Python me string ka matlab hai characters ki ek line jo order me judi hoti hai. Do main operators hote hain string banane ke liye. Pehla hai + yaani concatenation — yeh do strings ko jod deta hai, jaise "foot" + "ball" se "football" ban jaata hai. Dhyaan rahe, beech me space khud se nahi aata; tum jo doge bas wahi milega, isliye space chahiye to "foot" + " " + "ball" likhna padega.

Doosra hai * yaani repetition — yeh ek string ko n baar copy karke jod deta hai. "ab" * 3 ka result "ababab" hota hai. Yaad rakho count hamesha integer hona chahiye. "ab" * "3" chalega nahi kyunki "3" ek string hai, number nahi. Aur agar count zero ya negative ho, to result empty string "" aata hai — error nahi aati, yeh point exam me trap karta hai.

Sabse bada concept: strings immutable hoti hain. Iska matlab + ya * kabhi original string ko change nahi karte, balki ek nayi string return karte hain. Jab tum s = s + "x" likhte ho to purani string badalti nahi, ek nayi banti hai aur naam s usko point karne lagta hai.

Ek aur common galti: "3" + 4 kaam nahi karega, kyunki ek text hai aur doosra number — Python guess nahi karta. Pehle str(4) se number ko text banao, tab + chalega. Bas yeh do operators aur yeh do-teen rules cover kar lo, to roz ki 80% string building aa jaati hai.

Test yourself — Introduction to Programming (Python)

Connections