A string begins and ends with matching quote marks:
message = "Good morning"Line-by-line explanation
messageis the variable name.=stores a value.- The first quote begins the string.
Good morningis the text.- 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
- The outer double quotes begin and end the string.
- The apostrophe in
I'mis ordinary text because it does not match the outer quotes.
message = "She said, "Hello!""Line-by-line explanation
- The first quote begins the string.
\"adds a visible quote without ending the string.- The final quote without a backslash ends the string.
message = "First line\nSecond line"Line-by-line explanation
\nmeans “start a new line”.- 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
- The first line stores a string.
- The second line displays it.
Hint
- Keep double quotes around the full string.
- Use
\"for inner quotes. - Change only CHANGE ME.
Show Solution
quote = "Sam said, "Hi!""
print(quote)Line-by-line explanation
- The outer quotes contain the string.
- Each
\"creates a visible inner quote. - The second line displays the completed text.