Strings Basics
Learn how to create, combine, and work with text in Python — one of the most essential skills in any program.
Almost every program works with text — names, messages, addresses, commands. In Python, a piece of text is called a string. Understanding strings is one of the very first superpowers you will use as a programmer. This lesson shows you how to create them, combine them, measure them, and avoid a few sneaky traps.
See it in action
— step through the idea, then dive into the details below.Text in Python = Strings
Almost every program handles text — names, messages, commands. In Python, any piece of text is called a string, and it's one of the first superpowers you'll reach for.
#What is a String?
A String is a Necklace of Characters
Imagine threading letters onto a string of beads, one after another: H, e, l, l, o. The whole necklace is a string. Each bead is a single character. Python keeps them in order and remembers every one.
You create a string by wrapping text in quotes. Python accepts single quotes '...', double quotes "...", or triple quotes '''...''' / """...""". All three styles produce strings — choose whichever makes your code easiest to read.
#Single and Double Quotes
greeting = 'Hello, world!'
name = "Alice"
print(greeting)
print(name)There is no difference between single and double quotes in Python — pick one and be consistent. The real reason both exist is so you can include one type of quote inside the other without any trouble.
# Include an apostrophe using double quotes
message = "It's a great day!"
# Include double quotes using single quotes
quote = 'She said "hello" to everyone.'
print(message)
print(quote)#Triple-Quoted Strings
Triple quotes let you write a string that spans multiple lines without any extra tricks. Everything between the opening """ and closing """ (or ''') becomes part of the string, line breaks included.
poem = """
Roses are red,
Violets are blue,
Python is fun,
And so are you!
"""
print(poem)#Escape Sequences
Sometimes you need to put a special character inside a string — a line break, a tab, or a literal quote — but you cannot just type it directly. Python uses a backslash `\` as an escape signal: it tells Python that the next character has a special meaning.
The most common escape sequences are: - \n — newline (moves to the next line) - \t — tab (inserts a horizontal tab) - \\ — a literal backslash - \' — a single quote inside a single-quoted string - \" — a double quote inside a double-quoted string
print("Line one\nLine two")
print("Name:\tAlice")
print("C:\\Users\\Alice")
print('She said \'hi\'.')Windows File Paths and Backslashes
On Windows, folder paths use backslashes: C:\Users\Alice. If you type this directly in a string, Python will misread \U and \A as escape sequences. Always double the backslashes (C:\\Users\\Alice) or use a raw string: r'C:\Users\Alice'.
#Combining Strings with + and *
You can glue strings together with the `+` operator — this is called concatenation. You can also repeat a string a fixed number of times using the *`` operator**.
first = "Good"
rest = " morning!"
full = first + rest
print(full)
line = "-" * 20
print(line)You Cannot + a String and a Number
Python does not automatically convert numbers to text. "Score: " + 10 will raise a TypeError. You must convert the number first: "Score: " + str(10). This is a very common beginner mistake!
score = 42
message = "Your score is: " + str(score)
print(message)#Measuring Length with len()
len() is a built-in Python function that tells you how many characters are in a string — spaces and punctuation included. Think of it as counting every bead on that necklace.
word = "Python"
sentence = "Hello, world!"
print(len(word))
print(len(sentence))#Strings are Immutable
Here is something important: once a string is created, you cannot change any individual character inside it. This property is called immutability — strings are locked in place. If you want a different string, you create a new one.
name = "Alice"
# This will cause an error:
# name[0] = "B" <-- TypeError!
# The right way: build a new string
name = "B" + name[1:]
print(name)Why Are Strings Immutable?
Immutability makes strings safe and predictable. When you pass a string to a function, you know the original cannot be secretly changed. Python can also optimize memory by reusing identical strings behind the scenes.
Reassigning is Fine
You can always replace the whole string by assigning a new value to the variable: name = "Bob". That does not mutate the old string — it just points the variable at a brand-new one.
What will the following code print? ```python word = "hi" result = word * 3 + "!" print(result) ```
Key takeaways
- Strings can be created with single quotes, double quotes, or triple quotes — all work the same way.
- Escape sequences like `\n` (newline) and `\t` (tab) let you embed special characters inside a string.
- Use `+` to join strings and `*` to repeat them; always convert numbers with `str()` before concatenating.
- `len()` counts every character in a string, including spaces and punctuation.
- Strings are immutable — you cannot change a character in place; instead, build a new string.
What does this code print?
word = "hi"
result = word * 3 + "!"
print(result)This code is supposed to print "Score: 100" but it crashes. What is wrong?
score = 100
print("Score: " + score)Complete the code so it prints the number of characters in the word "Python".
word = "Python" print((word))
Put these lines in the right order to build a full name and print a greeting.
first = "Ada"
full_name = first + " " + last
print("Hello, " + full_name + "!")last = "Lovelace"
Create a variable called full_name by concatenating a first name and a last name with a space in between. Then print a message that reads: "Hello, [full_name]! Your name has [N] characters." — where N is the length of the full name (not counting the greeting itself).
Try it live — edit the code and hit Run to execute real Python: