Skip to main content

Text, Boolean, and None — Representing Meaning Beyond Numbers

What You'll Learn

In this lesson, you'll master:

  • Understanding strings (str) as sequences of characters with different quote styles
  • Using booleans (bool) for True/False decisions
  • Identifying truthy and falsy values across all Python types
  • Understanding None as representing the absence of value
  • Recognizing string immutability and its implications

Opening: Beyond Numbers

You've mastered numbers in the previous lesson. But programming isn't just about math—it's about representing all kinds of information. Consider these everyday examples:

  • Text: "Hello", "[email protected]", "Room 404"
  • True or False: Light switch ON/OFF, Fridge EMPTY/FULL, Answer YES/NO
  • Nothing: No milk available, no response received, no data found

These everyday examples represent three types of data that Python needs:

  • Text (str): "Hello", "[email protected]", "Room 404"
  • True/False (bool): Switch is ON, Fridge is EMPTY, Question is YES
  • Nothing (None): No milk, no response, no data

This lesson teaches you Python's three most expressive types: str (strings), bool (booleans), and None (absence). Together, they let you represent the richness of real-world meaning.


Concept 1: What Is a String (str)?

A string is a sequence of characters—letters, numbers, symbols, spaces, and punctuation all in order. It's how Python stores text.

Examples of Strings

name: str = "Alice"
email: str = "[email protected]"
greeting: str = "Hello, World!"
code: str = "ABC123"
empty: str = ""

Key characteristics:

  • A sequence: Made of individual characters in a string together
  • Immutable: Once created, you can't change individual characters (you create a new string instead)
  • Ordered: Position matters ("bat" ≠ "tab")
  • Can be empty: "" is a valid string with zero characters

Why Use Strings?

Strings represent any text data:

  • Names, emails, usernames
  • Messages, labels, descriptions
  • File paths, URLs, IDs
  • Anything that isn't a number or True/False

When you need to store, display, or process words or text, use str.

Quote Variations: Three Ways to Write Strings

Python gives you three ways to write strings, all equally valid. The choice depends on your content:

Single Quotes: 'hello'

name: str = 'Alice'
city: str = 'New York'

Use when your string contains double quotes:

quote: str = 'He said "Hello!"'  # Double quotes inside single-quoted string

Double Quotes: "hello"

name: str = "Alice"
city: str = "New York"

Use when your string contains single quotes (apostrophes):

phrase: str = "Don't worry"  # Apostrophe inside double-quoted string

Triple Quotes: '''...''' or """..."""

message: str = """Hello,
This is a multi-line string.
It can span several lines."""

poem: str = '''Roses are red
Violets are blue'''

Use for multi-line strings (text spanning multiple lines). Triple quotes preserve line breaks.

Key insight: All three quote types create strings. Pick whichever keeps your code readable:

# All of these are equivalent:
same1: str = "Alice"
same2: str = 'Alice'
same3: str = """Alice"""

print(same1 == same2) # True
print(same2 == same3) # True

String Immutability: You Can't Change Individual Characters

Immutable means unchangeable. Once a string is created, you cannot modify its individual characters.

name: str = "Alice"

# TRYING TO CHANGE IT (This doesn't work!)
# name[0] = "B" # ❌ Error: TypeError

If you want "Blice", you must create a new string:

name: str = "Alice"
new_name: str = "B" + name[1:] # Combine "B" with "lice" → "Blice"
print(new_name) # Blice
print(name) # Alice (original unchanged)

Why immutability matters: It prevents accidental changes to data and makes strings predictable. In AI-native development, immutability is a safety guarantee—your AI collaborator knows strings won't be modified unexpectedly.


Concept 2: What Is a Boolean (bool)?

A boolean is a data type with exactly two possible values: True or False. It's for yes/no, on/off, pass/fail decisions.

Examples of Booleans

is_student: bool = True
has_passed: bool = False
is_adult: bool = True
is_online: bool = False

Key Characteristics

  • Only two values: True or False (nothing else)
  • Capitalization matters: True and False (uppercase). Python will reject true or false
  • Represent logic: Answer yes/no questions, control program flow

Why Use Booleans?

Booleans answer yes/no questions:

  • Is the user logged in? True or False
  • Has the user paid? True or False
  • Is the product in stock? True or False
  • Is the password correct? True or False

When to choose bool: Any time you need to represent a binary (two-choice) state.

Code Example: Boolean Variables

# Status flags
is_student: bool = True
has_passed: bool = False
is_premium_member: bool = True
is_online: bool = False

# You can print them
print(is_student) # True
print(type(is_student)) # <class 'bool'>

Concept 3: Truthy and Falsy Values

Here's a powerful concept that prepares you for conditional statements (Chapter 17):

In Python, almost EVERY value can be treated as True or False. Some values are naturally "falsy" (act like False), and everything else is "truthy" (acts like True).

Understanding Falsy Values

These values are considered False:

ValueTypeMeaning
FalseboolLiterally the boolean False
0intZero (no count)
0.0floatZero (no measurement)
""strEmpty string (no text)
[]listEmpty list (no items)
{}dictEmpty dict (no key-value pairs)
NoneNoneTypeAbsence of value

Understanding Truthy Values

Everything else is True:

ValueTypeMeaning
TrueboolLiterally the boolean True
1, -5, 99intAny non-zero number
0.1, -3.14floatAny non-zero decimal
"hello"strAny non-empty string
[1, 2, 3]listAny non-empty list
{"a": 1}dictAny non-empty dict

Checking Truthiness with bool()

The bool() function converts any value to True or False:

print(bool(0))          # False
print(bool(1)) # True
print(bool("")) # False
print(bool("hello")) # True
print(bool([])) # False
print(bool([1, 2, 3])) # True
print(bool(None)) # False

Code Demonstration: Truthy/Falsy in Context

value1: int = 0
value2: str = ""
value3: int = 42
value4: str = "hello"

print(bool(value1)) # False (0 is falsy)
print(bool(value2)) # False ("" is falsy)
print(bool(value3)) # True (42 is truthy)
print(bool(value4)) # True ("hello" is truthy)

# This becomes useful in conditional statements (Chapter 17):
# if value3: # If truthy
# print("yes") # This will execute
# if value2: # If falsy
# print("no") # This will NOT execute

Why Truthy/Falsy Matters

Understanding truthy/falsy values prepares you for Chapter 17, where you'll learn to write conditional logic like:

  • Checking if a username is not empty (truthy check)
  • Verifying if age meets requirements (comparison)
  • Testing if a user has premium status (boolean check)

AI Prompt Used for this section: ""Create a Python script that demonstrates truthy and falsy values. Show examples of False-evaluating values (0, '', [], {}, None) and truthy values (1, 'hello', [1,2], {'a': 1}). Use bool() to convert each to True/False.""

Generated Code:

# Truthy and falsy demonstration
# (Full loop version requires Chapter 17 - Control Flow)

print("=== FALSY VALUES ===")
# Demonstrating each falsy value individually
print(f"{repr(False):20} → bool() = {bool(False)}")
print(f"{repr(0):20} → bool() = {bool(0)}")
print(f"{repr(0.0):20} → bool() = {bool(0.0)}")
print(f"{repr(''):20} → bool() = {bool('')}")
print(f"{repr([]):20} → bool() = {bool([])}")
print(f"{repr({}):20} → bool() = {bool({})}")
print(f"{repr(None):20} → bool() = {bool(None)}")

print("\n=== TRUTHY VALUES ===")
# Demonstrating each truthy value individually
print(f"{repr(True):20} → bool() = {bool(True)}")
print(f"{repr(1):20} → bool() = {bool(1)}")
print(f"{repr(-5):20} → bool() = {bool(-5)}")
print(f"{repr('hello'):20} → bool() = {bool('hello')}")
print(f"{repr([1, 2]):20} → bool() = {bool([1, 2])}")
print(f"{repr({'a': 1}):20} → bool() = {bool({'a': 1})}")

Validation Steps:

  • ✓ Ran on Windows, Mac, and Linux—works across platforms
  • ✓ Correctly identifies all falsy values evaluate to False
  • ✓ Correctly identifies all truthy values evaluate to True
  • ✓ Output format clear and easy to understand
  • ✓ No control flow statements (loops/conditionals taught in Chapter 17)

Concept 4: What Is None?

None is a special value representing absence—"nothing," "no data," "no result."

None Is NOT Zero or Empty

Many beginners confuse None with 0 or "". They're completely different:

value1: int = 0           # Zero: a number that exists (just happens to be 0)
value2: str = "" # Empty string: text that exists (just has no characters)
value3: None = None # None: no data at all

print(value1 == 0) # True: 0 is indeed zero
print(value2 == "") # True: "" is indeed empty
print(value3 is None) # True: None is specifically None

The semantic difference:

  • 0 = "The count is zero" (a count that exists)
  • "" = "The message is empty" (a string that exists but has no text)
  • None = "There is no value at all" (nothing exists)

None as a Singleton

There is exactly ONE None object in the entire Python program:

a: None = None
b: None = None

print(a is b) # True: both refer to the exact same None object
print(id(a) == id(b)) # True: same memory address

This singleton property means checking if x is None: is the correct Python idiom (not if x == None:).

Why Use None?

None represents missing or absent data:

  • Function has no result: return None
  • Optional parameter not provided: name: str | None = None
  • Data field is missing: phone: str | None = None
  • Placeholder for data you'll fill later: result: int | None = None

Examples of None

# Optional user phone (user might not provide it)
user_phone: str | None = None

# Function that might not return anything
def find_user(user_id: int) -> dict | None:
# Returns user dict if found, None if not found
...

# Checking for None (comparison without if - you'll learn if in Chapter 17)
is_none: bool = user_phone is None
print(f"Phone is None: {is_none}") # True

# When you have a value
user_phone = "555-1234"
is_none = user_phone is None
print(f"Phone is None: {is_none}") # False
print(f"Phone: {user_phone}") # Phone: 555-1234

Key syntax: Use str | None (read as "string or None") to indicate a value that could be text OR nothing.


Practice Exercise 1: Identify Truthy/Falsy Values

For each value below, determine whether it's truthy or falsy. Write your answer as T (truthy) or F (falsy):

# Your turn: Write T or F for each

1. False # ___
2. 0 # ___
3. "" # ___
4. "0" # ___ (hint: it's a string!)
5. [] # ___
6. [0] # ___ (non-empty list, even though it contains 0)
7. {} # ___
8. {"a": 0} # ___ (non-empty dict, even though value is 0)
9. None # ___
10. 42 # ___
11. -1 # ___
12. "hello" # ___
13. " " # ___ (space character)
14. 0.0 # ___
15. -0.0 # ___

Answers (check yourself after completing):

1. F  2. F  3. F  4. T  5. F  6. T  7. F  8. T  9. F  10. T
11. T 12. T 13. T 14. F 15. F

Practice Exercise 2: Choose the Right Type (str, bool, or None)

For each scenario, decide whether you'd use str, bool, or None:

# 1. A user's email address
email: ??? = "[email protected]"

# 2. Whether the user has confirmed their email
is_email_confirmed: ??? = True

# 3. A user's phone number (but they didn't provide one)
phone: ??? = ???

# 4. The user's favorite color
favorite_color: ??? = "blue"

# 5. Whether the user has a paid subscription
has_subscription: ??? = False

# 6. The result of searching for a user (might not exist)
search_result: ??? = None

# 7. A product's name
product_name: ??? = "Python Book"

# 8. Whether a product is in stock
in_stock: ??? = True

# 9. A product's description (user might not have entered one)
description: ??? = ???

# 10. Whether the order was completed
order_completed: ??? = True

Your answers:

1. email: str = "[email protected]"
2. is_email_confirmed: bool = True
# ... continue for 3-10

Practice Exercise 3: Fix the String Quote Error

The code below has quote-related issues. Fix them:

# BROKEN CODE - identify and fix the errors

# This will cause an error - fix it
quote1 = "He said "Hello"" # ❌ Nested quotes conflict

# This will cause an error - fix it
message = 'Don't worry' # ❌ Apostrophe inside single quotes

# This is multi-line but uses wrong quotes
poem = "Roses are red
Violets are blue" # ❌ Line breaks not allowed in regular quotes

# FIXED VERSION - write your corrections here:
quote1: str = ...
message: str = ...
poem: str = ...

Your corrections:

quote1: str = 'He said "Hello"'  # or: "He said \"Hello\""
message: str = "Don't worry" # or: 'Don\'t worry'
poem: str = """Roses are red
Violets are blue"""

Common Pitfalls

Pitfall 1: Forgetting Quote Matching

# WRONG - mismatched quotes
name: str = "Alice'

# RIGHT - matching quotes
name: str = "Alice"
# or
name: str = 'Alice'

Pitfall 2: Confusing True/False Capitalization

# WRONG - lowercase true/false
is_student: bool = true # ❌ NameError

# RIGHT - capitalize True/False
is_student: bool = True # ✅

Pitfall 3: Treating None Like Zero or Empty String

# WRONG - assuming None acts like 0 or ""
value: None = None
print(value == 0) # False (None is not zero)
print(value == "") # False (None is not empty string)
print(value is None) # True (None is specifically None)

# RIGHT - use 'is None' for checking absence
is_absent: bool = value is None
print(f"Value is absent: {is_absent}") # True (when value is None)

# You'll learn to use 'is None' in conditional statements in Chapter 17

Pitfall 4: Trying to Modify Strings

# WRONG - strings are immutable
word: str = "hello"
word[0] = "j" # ❌ TypeError: 'str' object does not support item assignment

# RIGHT - create a new string
word: str = "hello"
new_word: str = "j" + word[1:] # "jello"

Pitfall 5: Forgetting Triple Quotes for Multi-line

# WRONG - line breaks in regular quotes
text: str = "Line 1
Line 2" # ❌ SyntaxError

# RIGHT - use triple quotes
text: str = """Line 1
Line 2""" # ✅

Why This Matters: Semantic Clarity

Here's the big picture: Type hints communicate semantic meaning, not just syntax.

When you write:

user_name: str = "Alice"
is_premium_member: bool = True
phone_number: str | None = None

You're telling your AI collaborator (and future you):

  1. user_name is text representing a person's name
  2. is_premium_member is a yes/no decision about membership status
  3. phone_number could be a phone (text) or nothing (absent data)

This semantic clarity enables your AI collaborator to:

  • Suggest string operations (uppercase, split, validate email)
  • Understand boolean logic for decision-making
  • Handle optional fields correctly (default values, validation)

In AI-native development, clear type specifications are how you communicate intent—not just to Python, but to your AI partner.

🎓 Expert Insight

At scale (1000+ variables), type hints with semantic naming prevent entire classes of bugs. When you see confirmed: bool, you immediately know it's a yes/no decision. When you see phone: str | None, you know to handle the "no phone" case. This clarity becomes AI-readable specifications. Professional codebases (Stripe, Google, Meta) rely on type hints as executable documentation.


Try With AI

This section reinforces your learning by engaging with an AI tool to practice truthy/falsy values and None semantics.

Setup

Use ChatGPT web (chat.openai.com) or your AI companion from earlier chapters.

Prompt Set

Prompt 1: Truthy/Falsy Exploration

Copy and paste this prompt:

Create 20 examples of Python values and whether they're truthy or falsy.
Include various types: booleans, numbers, strings, empty collections, None.
Format: value → bool(value)

Example format:
0 → False
1 → True
"hello" → True
[] → False

Expected Outcome: AI generates a comprehensive list showing truthy/falsy patterns. You'll see that non-zero numbers are truthy, empty collections are falsy, and None is always falsy.


Prompt 2: None vs Zero vs Empty String

What's the practical difference between None, 0, and "" in Python?
When would you use each one? Give me 3 real-world scenarios where the
distinction matters.

Expected Outcome: AI explains semantic differences: None = "no data at all," 0 = "numeric zero exists," "" = "text exists but empty." You'll understand why checking if x is None: is more precise than if x == 0:.


Prompt 3: String Quote Styles

Show me examples of each string quote style in Python:
1. Single quotes
2. Double quotes
3. Triple quotes (both """ and ''')

For each, show when you'd prefer that style. What problem does each
quote style solve?

Expected Outcome: AI demonstrates quote options and practical use cases. You'll see that choosing quotes isn't about preference—it's about making code readable when strings contain apostrophes or quotation marks.


Prompt 4: Type Hints as Specifications (Stretch)

How do these type hints communicate different semantic meanings?

1. name: str = "Alice"
2. age: int | None = None
3. is_active: bool = True

For each, explain: What does the type hint tell you? What operations
would be safe? What would be dangerous?

Expected Outcome: AI explains that type hints are executable specifications. str says "text operations OK, math unsafe." int | None says "might be numeric or absent—check before using." bool says "decision/flag use only." This connects type hints to semantic meaning.


Safety & Ethics Note

When exploring truthy/falsy with AI, verify that:

  • The AI correctly identifies falsy values (False, 0, "", [], , None)
  • The AI correctly identifies truthy values (everything else, including non-zero numbers, non-empty strings, non-empty collections)
  • The AI recognizes that "0" (string zero) is truthy, while 0 (integer zero) is falsy
  • The AI explains is None as the correct idiom, not == None

Understanding truthy/falsy is critical because it's the foundation for conditional logic in Chapter 17. Make sure your AI examples align with these Python semantics.