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.