Lesson 3: Variables and Strings
Topics Covered:
- Declaring and using variables
- Working with text using strings
- Basic string operations and concatenation
What is a Variable?
A variable stores information that your program can use later. You can think of it as a box with a label.
name = "Alex"
age = 12
Using Strings
A string is any text inside quotation marks. You can use single ('
) or double ("
) quotes:
greeting = "Hello"
nickname = 'Buddy'
Combining Strings
You can combine strings using the +
operator:
name = "Alex"
print("Hello, " + name + "!")
Activity: Build a Sentence
Ask for a user’s name and favorite food, then print a custom message:
name = input("What is your name? ")
food = input("What is your favorite food? ")
print(name + " loves to eat " + food + ".")
Challenge
- Ask the user for their favorite