BeginnerLesson firstCategory 6 of 20

String Basics

Create and manipulate text strings. Read the lesson first, then move through the exercises in order.

8 Sections5 Exercises

After reading

Practice Arena

Begin with the first exercise, then continue step by step through the module.

Start with Quote Styles

Study Material

Read the full lesson

What is a string?

In programming, text is treated differently from numbers.

A sequence of characters—letters, spaces, punctuation, or even numbers treated as text—is called a string.

Think of it like a piece of yarn with letters strung across it. The computer does not try to do math with a string; it just remembers the exact characters in order.

Creating strings with quotes

To tell Python that something is a string, you wrap it in quotes.

python
name = "Alice" city = 'London'

You can use single quotes (') or double quotes ("). Python treats them exactly the same.

Why have two? It makes your life easier when your text contains a quote itself.

python
message = "It's a beautiful day" # Using double quotes outside so the single quote inside is safe quote = 'She said "Hello" to me' # Using single quotes outside

If you use the same quotes inside and out, Python gets confused about where the string ends.

Multi-line strings

Sometimes you want a string to stretch across multiple lines, like a poem or a long message.

For this, Python gives you triple quotes (""" or ''').

python
letter = """Dear Alice, It was great seeing you yesterday. Let's catch up again soon! Best, Bob""" print(letter)

Triple quotes preserve all the line breaks and spaces exactly as you type them.

Special characters

Sometimes you need to include characters that are hard to type normally, like a new line or a tab space.

Python uses a backslash \ as an escape character. It tells Python: "The next character is special."

The most common one is \n, which means "Insert a new line here".

python
print("First line\nSecond line")

Another useful one is \t, which inserts a tab space.

If you actually want to print a backslash, you have to escape the backslash itself by typing two of them: \\.

String concatenation (adding strings)

You can glue strings together using the + sign. This is called concatenation.

python
greeting = "Hello" name = "Maya" print(greeting + name)

The output will be HelloMaya.

Notice there is no space! Python does exactly what you tell it. If you want a space, you have to add it yourself.

python
print(greeting + " " + name)

Now the output is Hello Maya.

String repetition (multiplying strings)

You can also use the * sign to repeat a string.

python
laugh = "Ha" print(laugh * 3)

This will print HaHaHa.

This is incredibly useful when you want to draw lines or borders in your text output.

python
print("-" * 20) print("MAIN MENU") print("-" * 20)

Measuring length

Often, you need to know how many characters are in a string. For example, checking if a password is long enough.

Python has a built-in function called len() that counts them.

python
password = "mysecretcode" print(len(password))

This prints 12.

Remember that len() counts everything inside the quotes, including spaces and punctuation.

python
print(len("Hello World")) # Output: 11 print(len("")) # Output: 0 (an empty string)

What this lesson should give you

After this lesson, you should understand how to:

  • create strings using single, double, and triple quotes
  • solve problems when quotes appear inside your text
  • use \n to force a new line
  • combine strings with + and repeat them with *
  • measure the exact length of a string using len()

Strings are everywhere in programming. Whenever you write an email, send a text, or search on Google, you are passing strings to a computer!

Interactive

Exercises for this topic

These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.