Working with StringsBeginner8 min18 / 63

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

Visual walkthrough1 / 5
1

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.

Strings are everywhere: user input, file names, printed output — all strings.

#What is a String?

Think of it like

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

Single quotes and double quotes both work the same way.
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.

Mixing quote styles lets you include the other kind of quote inside your string.
# 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.

Triple quotes preserve every newline exactly as you typed it.
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

Escape sequences let you embed special characters inside any string.
print("Line one\nLine two")
print("Name:\tAlice")
print("C:\\Users\\Alice")
print('She said \'hi\'.')
Common mistake

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**.

+ joins strings together; * repeats a string.
first = "Good"
rest = " morning!"
full = first + rest
print(full)

line = "-" * 20
print(line)
Watch out

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!

Use str() to convert a number before concatenating it with a string.
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.

len() counts every character, including spaces and punctuation.
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.

You cannot modify a string in place. Instead, build and assign a new string.
name = "Alice"
# This will cause an error:
# name[0] = "B"   <-- TypeError!

# The right way: build a new string
name = "B" + name[1:]
print(name)
Note

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.

Tip

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.

Quick check

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.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
word = "hi"
result = word * 3 + "!"
print(result)
Fix the bug#2

This code is supposed to print "Score: 100" but it crashes. What is wrong?

fix-bug
score = 100
print("Score: " + score)
Fill in the blank#3

Complete the code so it prints the number of characters in the word "Python".

word = "Python"
print((word))
Reorder the lines#4

Put these lines in the right order to build a full name and print a greeting.

1
first = "Ada"
2
full_name = first + " " + last
3
print("Hello, " + full_name + "!")
4
last = "Lovelace"
Your turn
Practice exercise

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:

solution.py · editable