Day 2 of 80

Variables, Strings & F-Strings

Phase 1: Python Foundation

What You'll Build Today

Welcome to Day 2! Yesterday, you set up your environment and wrote your first line of code. Today, we are going to make that code dynamic.

We are going to build a Personalized Greeting Generator. Instead of a program that just says the same thing every time, your program will actually "listen" to the user, remember what they said, and generate a custom response.

Here is what you will learn and, more importantly, why you need to learn it:

* Variables: Because you need a way to save information (like a user's name) to use it later. Without variables, your code has no memory.

* Input/Print: Because software is a conversation. You need to ask questions (input) and give answers (print).

* Data Types (Strings vs. Integers): Because Python needs to know if "5" is a word or a number to do math with.

* F-Strings: Because sticking words and variables together manually is messy and prone to breaking. This is the secret weapon for building prompts for AI later.

* String Methods: Because users input messy data (like typing their name in all lowercase), and you need tools to clean it up automatically.

Let's get started.

The Problem

Before we look at the solution, let's look at why we need one.

Imagine you want to greet three different users: Alice, Bob, and Charlie. With the tools you have right now (just the print statement), your code would look like this:

# The "Hardcoded" Way

print("Hello, Alice! Welcome to the AI Bootcamp.")

print("We are so glad to have you, Alice.")

print("Good luck with your coding, Alice!")

print("Hello, Bob! Welcome to the AI Bootcamp.")

print("We are so glad to have you, Bob.")

print("Good luck with your coding, Bob!")

print("Hello, Charlie! Welcome to the AI Bootcamp.")

print("We are so glad to have you, Charlie.")

print("Good luck with your coding, Charlie!")

Feel the pain here?
  • Repetition: You wrote the exact same sentences three times.
  • Maintenance Nightmare: If you decide to change "AI Bootcamp" to "GenAI Course," you have to find and replace it in every single line.
  • No Interactivity: If a fourth person named "Sarah" shows up, the program doesn't know her. You have to open the code file, write three new lines, and save it again.
  • This is called Hardcoding. It is rigid, brittle, and impossible to scale. If you were building an AI chatbot, you couldn't possibly hardcode a response for every single thing a human might say.

    There has to be a way to write the sentence template once and just swap out the name.

    Let's Build It

    We are going to solve this by using Variables (containers for data) and F-Strings (a clean way to format text).

    Step 1: Creating a Variable

    Think of a variable as a labeled box. You can put data inside the box, and whenever you use the label, Python looks inside the box to see what is there.

    In Python, we create a variable just by giving it a name and using the = sign.

    # We create a variable named 'user_name' and put the text "Alice" inside it.
    

    user_name = "Alice"

    # Now we can use the variable in print statements

    print(user_name)

    Output:
    Alice
    

    If we change the content of the variable, the output changes, even though the print command stayed the same.

    user_name = "Bob"
    

    print(user_name)

    Output:
    Bob
    

    Step 2: Accepting User Input

    Right now, we are still typing "Alice" or "Bob" into the code. Let's make the program ask the user for their name. We use the input() function for this.

    When Python sees input(), it pauses the program and waits for the user to type something and hit Enter.

    # The program asks for input, and stores whatever the user types into 'user_name'
    

    user_name = input("What is your name? ")

    print("You entered:")

    print(user_name)

    Run this code. You will see the cursor waiting for you. Type your name and hit Enter.

    Step 3: Mixing Text and Variables (The Old Way vs. F-Strings)

    Now we have the name stored. We want to say "Hello, [Name]!".

    In older versions of Python, you had to "glue" strings together using the + sign. This is called concatenation.

    # The Old, Painful Way
    

    user_name = "Alice"

    print("Hello, " + user_name + "! Welcome to the course.")

    Look closely at that code. Do you see the space after the comma inside the quote? "Hello, " . If you forget that space, the computer prints Hello,Alice. It is very easy to make formatting mistakes this way.

    The Solution: F-Strings

    Python introduced "f-strings" (formatted strings) to solve this. You put the letter f right before the opening quote, and then you can put your variables directly inside the text using curly brackets {}.

    # The Modern Way (F-Strings)
    

    user_name = input("What is your name? ")

    course_name = "GenAI Bootcamp"

    # Notice the 'f' before the quote and the {} around the variables

    print(f"Hello, {user_name}! Welcome to the {course_name}.")

    Output (if you type Sarah):
    Hello, Sarah! Welcome to the GenAI Bootcamp.
    

    This is much easier to read. You can see exactly what the sentence will look like.

    Step 4: Cleaning up Text (String Methods)

    Users are unpredictable. If you ask for a name, they might type " alice " (with spaces) or "ALICE". This looks bad in a sentence.

    Strings (text) in Python have built-in "methods"—little tools attached to the data that can modify it.

    * .strip() removes spaces from the start and end.

    * .lower() turns everything to lowercase.

    * .upper() turns everything to uppercase.

    * .title() capitalizes the first letter of each word.

    Let's chain these together to clean up our input.

    raw_name = input("What is your name? ")
    
    # Clean it up: remove spaces, then make it Title Case (e.g., "alice" -> "Alice")
    

    clean_name = raw_name.strip().title()

    print(f"Welcome, {clean_name}!")

    Step 5: Handling Numbers (Integers)

    Finally, let's ask for their age. There is a catch here. The input() function always returns a String (text). Even if the user types "25", Python sees it as the text word "25", not the number 25.

    If you try to do math on text, Python will panic.

    The Broken Code:
    age = input("How old are you? ")
    # This will crash because you can't add a number to a word
    

    next_year = age + 1

    print(f"Next year you will be {next_year}")

    The Fix:

    We must convert the string into an Integer (a whole number) using int().

    age_text = input("How old are you? ")
    

    age_number = int(age_text) # Convert text to number

    next_year = age_number + 1

    print(f"Next year you will be {next_year}.")

    Final Code: The Personalized Greeting Generator

    Here is the complete code combining everything we learned.

    print("--- PERSONALIZED GREETING GENERATOR ---")
    
    # 1. Get user input
    

    first_name = input("What is your first name? ")

    current_year = input("What is the current year? ")

    birth_year = input("What year were you born? ")

    # 2. Process the data # Clean the name strings

    clean_name = first_name.strip().title()

    # Convert years to integers so we can do math

    current_year_int = int(current_year)

    birth_year_int = int(birth_year)

    # Calculate age

    age = current_year_int - birth_year_int

    # 3. Output the result using an f-string

    print(f"Hello, {clean_name}!")

    print(f"Based on your birth year, you turn {age} years old in {current_year}.")

    Now You Try

    Take the "Personalized Greeting Generator" code above and modify it to complete these three tasks. This will help you get comfortable with the syntax.

  • The Shouting Bot: Modify the code so that when it prints the user's name, it prints it in ALL CAPS (using .upper()), regardless of how the user typed it.
  • The Decade Calculator: Add a new variable that calculates what year it will be in 10 years. Add a print statement: In 10 years, the year will be [Year].
  • The City Greeter: Add a new input asking the user for their city. Add a print statement that says I hear the weather in [City] is nice!
  • Challenge Project: The Mad Libs Generator

    "Mad Libs" is a word game where one player asks another for a list of words (a noun, a verb, an adjective, etc.) without telling them the context. Then, those words are inserted into a story template, usually with funny results.

    Your challenge is to build a Python version of this.

    Requirements:

    * Create a header using print that welcomes the user to the game.

    * Ask the user for at least 4 different inputs (e.g., a noun, a verb, an adjective, a famous person).

    * Store these inputs in clearly named variables.

    * Use an f-string to combine these variables into a short story (2-3 sentences).

    * Print the final story to the screen.

    Example Input/Output: Program:
    Please enter a noun: toaster
    

    Please enter a verb: explode

    Please enter an adjective: squishy

    Please enter a famous person: Batman

    Result:
    Yesterday, Batman walked into the room holding a squishy toaster.
    

    Suddenly, he decided to explode! It was a very strange day.

    Hint:

    Write your story on a piece of paper first. Circle the words you want the user to provide. Those circled words become your variables inside the { curly brackets }.

    What You Learned

    Today you moved from static code to dynamic code. You learned:

    * Variables: name = "Value" (Storing data for later).

    * Strings: Text data that we can manipulate using methods like .strip() and .upper().

    * Integers: Numbers that we can do math with.

    * Input: input("Prompt") (Getting data from the user).

    * F-Strings: f"Hello {name}" (Injecting variables into text).

    Why This Matters for AI:

    You might be thinking, "I want to build AI, why am I making Mad Libs?"

    Large Language Models (like ChatGPT) are essentially giant text processing engines. They take a string of text (your prompt) and predict the next string of text.

    When you build an AI application, you rarely type the prompt manually. You build the prompt dynamically using code.

    Example of what you will build later in this bootcamp:

    user_topic = input("What do you want to learn? ")
    # This is an f-string constructing a prompt for an AI!
    

    ai_prompt = f"Explain {user_topic} to me like I am five years old."

    If you can't manipulate strings and variables, you can't control the AI.

    Tomorrow:

    Right now, our code runs from top to bottom, doing the exact same steps every time. But what if we want the code to make a choice? What if we want to say "Good Morning" if the user types "AM" and "Good Evening" if they type "PM"? Tomorrow, we cover Logic and Decision Making.