Bird
Raised Fist0
Pythonprogramming~10 mins

String creation and representation in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - String creation and representation
Start
Choose quotes: '', "", ''' ''' or """ """
Write characters inside quotes
Python creates string object
String stored in variable or used directly
String can be printed or manipulated
End
This flow shows how Python creates a string by enclosing characters in quotes, stores it, and then uses it.
Execution Sample
Python
s1 = 'Hello'
s2 = "World"
s3 = '''Multi
line'''
print(s1)
print(s2)
print(s3)
Creates three strings with different quotes and prints them, showing single, double, and multiline strings.
Execution Table
StepActionCode LineVariable StateOutput
1Create string s1 with single quotess1 = 'Hello's1 = 'Hello'
2Create string s2 with double quotess2 = "World"s2 = 'World'
3Create multiline string s3 with triple quotess3 = '''Multi line'''s3 = 'Multi line'
4Print s1print(s1)s1 = 'Hello'Hello
5Print s2print(s2)s2 = 'World'World
6Print s3print(s3)s3 = 'Multi line'Multi line
💡 All strings created and printed successfully.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
s1undefined'Hello''Hello''Hello''Hello'
s2undefinedundefined'World''World''World'
s3undefinedundefinedundefined'Multi line''Multi line'
Key Moments - 2 Insights
Why can we use single, double, or triple quotes to create strings?
Python allows different quotes to make it easier to include quotes inside strings without extra symbols. See steps 1-3 in execution_table.
What does the inside the triple-quoted string do?
It creates a new line inside the string, so when printed (step 6), the output shows two lines.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of s3?
A'Multiline'
B'Multi line'
C'Multi line'
D'Multi\line'
💡 Hint
Check the Variable State column at step 3 for s3.
At which step is the string s2 created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the Action and Code Line columns to find when s2 is assigned.
If we change s1 to use double quotes instead of single quotes, how does the execution_table change?
AOutput changes
BVariable state changes to double quotes
CNo change in variable state, only code line changes
DExecution stops with error
💡 Hint
Quotes style does not affect the string content or output, only the code line text.
Concept Snapshot
String creation in Python:
- Use single ('') or double ("") quotes for simple strings
- Use triple quotes (''' ''' or """ """) for multiline strings
- Strings store text exactly as typed
- \n creates a new line inside strings
- Strings can be printed or used in code directly
Full Transcript
This lesson shows how Python creates strings by enclosing text in quotes. You can use single, double, or triple quotes. Triple quotes allow multiline text with new lines. Each string is stored in a variable and can be printed. The execution table traces each step: creating strings s1, s2, s3, and printing them. The variable tracker shows how variables get their values step by step. Key moments explain why different quotes are allowed and how new lines work inside strings. The quiz tests understanding of string values, creation steps, and quote usage. This helps beginners see exactly how strings are made and used in Python.

Practice

(1/5)
1. Which of the following is a correct way to create a string in Python?
easy
A. text = Hello'
B. text = Hello
C. text = 'Hello'
D. text = """Hello

Solution

  1. Step 1: Check string delimiters

    Python strings must be enclosed in matching quotes: single ('') or double (\"\").
  2. Step 2: Validate each option

    text = 'Hello' uses single quotes correctly. text = Hello misses quotes. text = Hello' has mismatched quotes. text = """Hello has unclosed triple quotes.
  3. Final Answer:

    text = 'Hello' -> Option C
  4. Quick Check:

    Strings need matching quotes = A [OK]
Hint: Strings always need matching quotes around text [OK]
Common Mistakes:
  • Forgetting quotes around text
  • Using mismatched quotes
  • Leaving quotes unclosed
2. Which of the following is the correct syntax to create a multi-line string in Python?
easy
A. text = '''Hello World'''
B. text = "Hello\nWorld"
C. text = 'Hello\nWorld'
D. text = '''Hello\nWorld'''

Solution

  1. Step 1: Understand multi-line string syntax

    Triple quotes (''' or """) allow writing strings across multiple lines without escape characters.
  2. Step 2: Analyze options

    Options A and B use single/double quotes with escaped newline, which works but is not multi-line string creation. text = '''Hello\nWorld''' uses triple quotes but escapes newline, so it shows literal \n. text = '''Hello World''' uses triple quotes with actual newline, creating a true multi-line string.
  3. Final Answer:

    text = '''Hello World''' -> Option A
  4. Quick Check:

    Triple quotes with real newlines = C [OK]
Hint: Use triple quotes and press Enter for multi-line strings [OK]
Common Mistakes:
  • Using single or double quotes for multi-line text
  • Escaping newlines inside triple quotes
  • Confusing escaped \n with actual newlines
3. What will be the output of the following code?
text = '''Hello
World'''
print(text)
medium
A. Hello\nWorld
B. Hello World
C. Hello World
D. SyntaxError

Solution

  1. Step 1: Understand triple-quoted string behavior

    Triple quotes preserve newlines inside the string as actual line breaks.
  2. Step 2: Analyze the string content and print output

    The \n escape sequence in the triple-quoted string is interpreted as a newline character, so the string content is 'Hello' + newline + 'World'. Thus, print(text) outputs two lines: 'Hello' then 'World' on the next line.
  3. Final Answer:

    Hello World (two lines) -> Option B
  4. Quick Check:

    Triple quotes keep newlines = A [OK]
Hint: Triple quotes print newlines as line breaks, not \n text [OK]
Common Mistakes:
  • Confusing escaped \n with actual newline
  • Expecting literal \n to print as two characters
  • Thinking triple quotes cause syntax error
4. The following code throws an error. What is the problem?
text = 'It's a sunny day'
medium
A. Missing escape for single quote inside string
B. Using double quotes instead of single quotes
C. Triple quotes needed for single quotes inside
D. No error, code is correct

Solution

  1. Step 1: Identify the string delimiter and content

    The string uses single quotes to start and end, but contains an unescaped single quote inside (in "It's").
  2. Step 2: Understand why error occurs

    The inner single quote ends the string early, causing a syntax error. It needs to be escaped with a backslash or use double/triple quotes.
  3. Final Answer:

    Missing escape for single quote inside string -> Option A
  4. Quick Check:

    Escape inner quotes or use different quotes = D [OK]
Hint: Escape inner quotes or use different quote types [OK]
Common Mistakes:
  • Ignoring escape for inner quotes
  • Assuming single quotes inside single quotes are allowed
  • Not using triple quotes for complex strings
5. You want to create a dictionary where keys are words and values are their lengths from a multi-line string. Which code correctly creates this dictionary?
text = '''apple\nbanana\ncherry\n'''
# Fill in the blank:
d = {word: len(word) for word in _____}
hard
A. text.split()
B. text.split(' ')
C. text.split('\n')
D. text.splitlines()

Solution

  1. Step 1: Understand the string and splitting methods

    The string ends with a newline ('''apple\nbanana\ncherry\n'''). splitlines() and split('\n') include a trailing empty string ''. split() splits on all whitespace (including newlines) and discards empty fields. split(' ') ignores newlines.
  2. Step 2: Choose the method that avoids extra empty key

    split() produces exactly {'apple':5, 'banana':6, 'cherry':6} without '' : 0.
  3. Final Answer:

    text.splitlines() -> Option D
  4. Quick Check:

    splitlines() handles newlines cleanly = B [OK]
Hint: Use splitlines() to split on newlines cleanly [OK]
Common Mistakes:
  • Using split() which discards empty lines but may split on other whitespace
  • Using split('\n') which includes trailing empty string
  • Using split(' ') which misses newlines