Strings: Text, Quotes, and Length

What you’ll learn in this lesson

  • What a string is
  • Single quotes, double quotes, and when each is handy
  • How to join strings together
  • How to repeat a string
  • How to count a string’s length and read one character

What a string is

A string is text: any sequence of characters — letters, digits, spaces, punctuation — wrapped in quotes. "Hello", 'Sam', "I am 9 years old" are all strings. The quotes are what tell Python “this is text, not a name and not a number.”

message = "Hello, world!"
print(message)

Output:

Hello, world!

A string can hold digits too: "42" is a string, not a number. That is why Lesson 5’s "5" + "3" gave "53": Python treated both values as text. If you need arithmetic, the value must be a number without quotes.


Single quotes or double quotes?

Both work, and they mean exactly the same thing. The trick is picking the kind that does not clash with the text itself:

print('Sam said "Hi!"')
print("It's a sunny day.")

Output:

Sam said "Hi!"
It's a sunny day.

The first line uses single quotes on the outside so the double quotes inside are safe. The second uses double quotes outside so the apostrophe in It’s is safe. Choose whichever keeps your text intact; there is no other difference.


Joining and repeating strings

+ joins strings end to end. This is called concatenation:

first = "Hello"
second = "Sam"
print(first + " " + second)

Output:

Hello Sam

Note the " " in the middle: without it, you would get HelloSam, because Python joins exactly what you give it and adds nothing on its own. Spaces, commas, and punctuation are your job.

* repeats a string:

print("Ha! " * 3)

Output:

Ha! Ha! Ha!

That is the same * as multiplication — with text, it means “repeat.”

A string and a number do not join. Try it and Python stops with an error:

print("You are " + 9)

Output:

TypeError: can only concatenate str (not "int") to str

Python will not guess what you mean. Turn the number into text first with str(): print("You are " + str(9)). Or use a comma, the way Lesson 4 showed: print("You are", 9) prints You are 9.


Length, and reading one character

len() counts how many characters a string has, including spaces:

word = "hello world"
print(len(word))

Output:

11

Count it yourself: h-e-l-l-o (5), one space (6), w-o-r-l-d (11). Spaces count, which is the detail beginners most often miss.

You can also read a single character by its position. Counting starts at 0, not 1 — the first character is position 0:

word = "Sam"
print(word[0])
print(word[1])
print(word[2])

Output:

S
a
m

word[0] is S, word[1] is a, word[2] is m. Asking for a position past the end, like word[3], gives an error — IndexError: string index out of range — because there is no fourth character. Zero-based counting feels strange for a day and then becomes second nature — most popular languages count this way.

The last character is at position len(word) - 1 — one less than the length, because counting starts at 0:

word = "Sam"
print(word[len(word) - 1])

Output:

m

Putting it together: a friendly message

Strings, variables, and joining combine into the kind of line you see in real programs:

name = "Mia"
print("Hello, " + name + "! You are learning Python.")

Output:

Hello, Mia! You are learning Python.

Joining with + is the foundation, and it always works.


Strings: Text, Quotes, and Length

Python for Absolute Beginners · Lesson 6

What you’ll learn

  • What a string is
  • Single quotes, double quotes, and when each is handy
  • How to join strings together
  • How to repeat a string
  • How to count a string's length and read one character
1 / 16

What a string is

  • A string is text: letters, digits, spaces, punctuation — wrapped in quotes
  • The quotes tell Python “this is text, not a name and not a number”
  • “42” is a string, not a number — that is why “5” + “3” gave “53” in Lesson 5
  • If you need arithmetic, the value must be a number without quotes
2 / 16

A string in code

message = "Hello, world!"
print(message)

Output:
Hello, world!

The quotes mark the value as text.

3 / 16

Single quotes or double quotes?

  • Both work, and they mean exactly the same thing
  • Pick the kind that does not clash with the text itself
  • Single quotes outside keep double quotes inside safe
  • Double quotes outside keep an apostrophe, like in It’s, safe
4 / 16

Quotes in code

print('Sam said "Hi!"')
print("It's a sunny day.")

Output:
Sam said "Hi!"
It's a sunny day.

Choose whichever keeps your text intact; there is no other difference.

5 / 16

Joining and repeating strings

  • + joins strings end to end — this is called concatenation
  • Python adds nothing on its own: spaces, commas, and punctuation are your job
  • * repeats a string — the same * as multiplication, but with text it means “repeat”
6 / 16

Joining strings in code

first = "Hello"
second = "Sam"
print(first + " " + second)

Output:
Hello Sam

Without the ” ” in the middle, you would get HelloSam.

A string and a number do not join: print(“You are ” + 9) gives TypeError: can only concatenate str (not “int”) to str. Fix it with str(9), or use a comma: print(“You are”, 9).

7 / 16

Repeating a string in code

print("Ha! " * 3)

Output:
Ha! Ha! Ha!

With text, * means “repeat.”

8 / 16

Length, and reading one character

  • len() counts how many characters a string has, including spaces
  • Spaces count — the detail beginners most often miss
  • You can read a single character by its position — and counting starts at 0, not 1
  • Asking for a position past the end gives an error
9 / 16

Length in code

word = "hello world"
print(len(word))

Output:
11

h-e-l-l-o (5), one space (6), w-o-r-l-d (11).

10 / 16

Reading one character in code

word = "Sam"
print(word[0])
print(word[1])
print(word[2])
print(word[len(word) - 1])

Output:
S
a
m
m

word[0] is S, word[1] is a, word[2] is m. The last character is at position len(word) – 1, which prints m. Asking for word[3] gives IndexError: string index out of range.

11 / 16

Putting it together

name = "Mia"
print("Hello, " + name + "! You are learning Python.")

Output:
Hello, Mia! You are learning Python.

Strings, variables, and joining combine into the kind of line real programs use.

12 / 16

Hands-on exercise

  • Store your first name in a variable and print a greeting built with +
  • Print the length of your name with len() — count by hand and check they match
  • Print the first and last characters of your name using positions (counting starts at 0 — and the last character is at position len(name) – 1)
  • Try “Boo! ” * 5 in the shell and explain the result in one sentence
13 / 16

Key takeaways

  • A string is text in quotes; “42” is text, not a number
  • Single and double quotes mean the same thing — pick the kind that does not clash with your text
  • + joins strings; * repeats them; Python adds no spaces for you
  • len() counts characters, including spaces
  • Positions start at 0: word[0] is the first character
14 / 16

Take the quiz

Take the Lesson 6 quiz — 5 quick questions to check what you learned.

15 / 16

Next lesson

Talking to Your Program: input()

16 / 16

Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.

Download the Lesson 6 slides (PowerPoint, with narration)

Download the Lesson 6 slides (PDF)

Hands-on exercise

  1. Store your first name in a variable and print a greeting built with +, like the example above.
  2. Print the length of your name with len(). Count by hand and check they match.
  3. Print the first and last characters of your name using positions (remember: counting starts at 0 — and the last character is at position len(name) - 1).
  4. Try "Boo! " * 5 in the shell and explain the result in one sentence.

Lesson 06: Strings: Text, Quotes, and Length

A string is text in quotes. len() counts its characters and + joins strings together. Change the word and run again.

Loading Python… this can take a few seconds the first time. Please wait — the Run button will become available when Python is ready.

Try this: Change the word to a longer word and press Run again — the length number changes too.

The code that runs



Output

Press Run to see the output here.

Practice file

Save this as lesson06-practice.py and run it with F5 in IDLE.

# Lesson 6: Strings — Text, Quotes, Length
# WHAT TO DO: A string is text inside quotes. This program stores a
# name and should build a greeting by joining strings with +.
# Fill in line 11: replace the placeholder word with the variable name
# (no quotes around it), so the greeting uses the stored name.
# HINT: Joining "Hello, " and the name gives "Hello, Sam". A variable
# name on its own means "use the value stored under that name".
# Run with F5 in IDLE. You are done when you change `name` to your own name, run again, and the greeting changes too.
name = "Sam"
greeting = "Hello, " + "TODO"  # <-- CHANGE THIS LINE: use the name variable
print(greeting)
print(len(name))

Key takeaways

  • A string is text in quotes; "42" is text, not a number.
  • Single and double quotes mean the same thing — pick the kind that does not clash with your text.
  • + joins strings; * repeats them; Python adds no spaces for you.
  • len() counts characters, including spaces.
  • Positions start at 0: word[0] is the first character.

Next lesson: Talking to Your Program: input().


Quick quiz

  1. What is a string?
  2. Why might you use double quotes instead of single quotes?
  3. What does "Hello" + " " + "Sam" give you?
  4. What is len("hello world")? Does the space count?
  5. In the string "Sam", what is position 0, and what happens if you ask for position 3?

Answer key: 1. Text wrapped in quotes. 2. To let the other kind of quote appear safely inside the text (for example, an apostrophe inside double quotes). 3. "Hello Sam" — without the space string you would get "HelloSam". 4. 11, and yes, the space counts as a character. 5. Position 0 is S; position 3 is an error, because there is no fourth character.