Python · Stage 1 · Storing Information · Lesson 10 of 50

Text and Strings

Create text with quotes and include quote marks or new lines safely.

A string begins and ends with matching quote marks:

message = "Good morning"
Line-by-line explanation
  1. message is the variable name.
  2. = stores a value.
  3. The first quote begins the string.
  4. Good morning is the text.
  5. The final quote ends the string.

Python accepts single or double quotes. Double quotes make an apostrophe easy to include:

message = "I'm learning Python"
Line-by-line explanation
  1. The outer double quotes begin and end the string.
  2. The apostrophe in I'm is ordinary text because it does not match the outer quotes.
message = "She said, "Hello!""
Line-by-line explanation
  1. The first quote begins the string.
  2. \" adds a visible quote without ending the string.
  3. The final quote without a backslash ends the string.
message = "First line\nSecond line"
Line-by-line explanation
  1. \n means “start a new line”.
  2. Displaying the string puts the two parts on separate lines.

Tiny Practice: Quoted Speech

Store Sam said, "Hi!" and display it.

Starter template

quote = "CHANGE ME"
print(quote)
Line-by-line explanation
  1. The first line stores a string.
  2. The second line displays it.
Hint
  1. Keep double quotes around the full string.
  2. Use \" for inner quotes.
  3. Change only CHANGE ME.
Show Solution
quote = "Sam said, "Hi!""
print(quote)
Line-by-line explanation
  1. The outer quotes contain the string.
  2. Each \" creates a visible inner quote.
  3. The second line displays the completed text.