String Fundamentals
Beginner · 16 min read · ▶ live playground · ✦ checkpoint
A scoreboard can print a player's name, a chant, and a two-line victory note with the same Python type. A string is text data Python stores as a str, and string fundamentals are the rules for writing that text exactly: quotes, joining, repeating, backslash codes, and raw strings.
Quotes mark the text
A string literal is a string written directly in your code, between quote marks. Think of the opening quote and closing quote like fence posts: Python treats everything between the matching posts as text.
Single quotes and double quotes both create strings. They are the same type. The useful habit is to choose the quote form that makes the text easiest to read.
player = "Mina"
team = 'Red Comets'
sign = """Mina's sign says:
"Keep going."
Score: 42"""
print(player)
print(team)
print(sign)MinaRed CometsMina's sign says:"Keep going."Score: 42
The player string uses double quotes. The team string uses single quotes. Python does not care which one you pick, as long as the closing quote matches the opening quote. After Python creates the string object, the name tag model from lesson 2.1 still applies: the variable name now refers to that text object.
The sign string uses triple quotes, three quote marks that create one string and allow real line breaks inside it. Triple-quoted strings are handy for short blocks of text you want to print with the same line breaks you see in the code.
Quote choice also helps when your text contains a quote mark. "Mina's turn" works cleanly because the apostrophe is not the double quote that closes the string. 'Coach said "ready"' works for the same reason in reverse.
The quote marks are not part of the value unless you put quote marks inside the string. They are instructions to Python, like the fence posts. The text between the posts is the thing you keep.
nickname = "Ace"
print(nickname)
print("nickname")Acenickname
The first print() uses the value referred to by the name nickname. The second print() prints the literal text "nickname". Python follows the quotes exactly, so those two lines do different jobs.
Join and repeat strings
Concatenation means joining strings end to end with +. Picture two strips of paper clipped together: Python does not add a gap, trim anything, or guess where words should separate.
Repetition means making a new string by copying the same string several times with *. It is like stamping the same label again and again.
player = "Mina"
badge = "string rookie"
message = player + " earns the " + badge + " badge"
divider = "-" * 30
chant = "Mina!" * 3
print(divider)
print(message)
print(chant)
print(divider)------------------------------Mina earns the string rookie badgeMina!Mina!Mina!------------------------------
That chant is squeezed together on purpose. Python repeated exactly the string "Mina!". If you want spaces, the spaces must be inside the string you join or repeat.
Both sides of + need to be strings for concatenation. If you have a number, convert it with str() from lesson 2.4, or use the f-strings you already met there.
One useful way to read + here is "make a new string from these pieces." It does not edit the old strings. player, badge, and message are three names referring to three string objects. That should feel familiar: strings are values, and assignment still means "now refers to."
Try it - build a tiny banner
Run this, then change the player's name, the mood, and the border length. Watch how every space and every repeated character changes the final text.
Backslashes give text tiny instructions
The backslash character, written \, can tell Python that the next character has a special meaning. An escape sequence is a backslash code inside a string that Python turns into one special character.
Three escape sequences are worth knowing now:
\ncreates a newline, the invisible character that moves output to the next line.\tcreates a tab, a spacing character that jumps forward to a wider gap.\\creates one real backslash character.
You can also escape a quote mark when you want the same quote mark inside the string.
score_note = "Mina\n42 points"
practice_log = "Name:\tMina"
quoted = "Coach said, \"Stay ready.\""
slash_note = "One \\ character"
print(score_note)
print(practice_log)
print(quoted)
print(slash_note)Mina42 pointsName: MinaCoach said, "Stay ready."One \ character
Escape sequences are tiny stage directions inside the text. You write two characters, such as backslash then n, but Python stores one special character: a line break.
This is why strings can look different in code than they look on the screen. The code line "Mina\n42 points" creates one string in memory, but printing it shows two lines. The stage direction runs when Python creates the string; print() then receives text that already contains a newline character.
Raw strings keep backslashes ordinary
A raw string is a string literal with r before the opening quote, so Python treats backslashes as ordinary text instead of escape starters. It keeps escape sequences like \n visible, but a raw string still cannot end with a single backslash. Use one when backslashes are data, not stage directions.
A common case is a path, a text address for a file or folder. Windows paths use many backslashes, and raw strings keep them readable.
doubled_path = "C:\\new_games\\team"
raw_path = r"C:\new_games\team"
regular_note = "Line 1\nLine 2"
raw_note = r"Line 1\nLine 2"
print(doubled_path)
print(raw_path)
print(regular_note)
print(raw_note)C:\new_games\teamC:\new_games\teamLine 1Line 2Line 1\nLine 2
Those two strings print the same text. The first one uses doubled backslashes because each \\ becomes one backslash. The second one says, "read the backslashes as ink on the page."
The note lines show the tradeoff. In regular_note, \n becomes a real newline. In raw_note, the backslash and n stay visible. Use a raw string when backslashes should stay visible. Use a regular string when you want escape sequences like \n and \t to become real newlines and tabs.
Checkpoint
Answer all three to mark this lesson complete
You can now make text behave exactly: one line, many lines, joined, repeated, or full of real backslashes. Next, you stop treating a string as one solid block and learn how Python reaches each letter and symbol inside it.