Python for Model Behavior and Prompt Engineering
Python Syntax and Text
Your First Python Program
The most basic task in many programming languages is displaying a message. In Python, we use the print() function to do this. A function is just a named chunk of code that performs a specific action. The print() function's action is to show output on the screen.
To tell print() what to display, we put the content inside the parentheses. When that content is text, we need to wrap it in quotes. In programming, a piece of text is called a string.
print("Hello, content designer!")
Run that code, and you'll see Hello, content designer! appear as output. You can use single quotes (') or double quotes ("); Python doesn't mind, as long as you use the same kind to open and close the string.
Storing and Labeling Text
Writing the same text over and over is tedious. Instead, we can store it in a variable. Think of a variable as a labeled box where you can keep a piece of information. You give the box a name (the variable name) and put your data inside it using the equals sign (=), which is called the assignment operator.
user_name = "Alex"
prompt_instruction = "Summarize the following text for a fifth-grader."
print(user_name)
print(prompt_instruction)
Here, we created two variables. user_name holds the string "Alex", and prompt_instruction holds our summary instruction. When we print the variable, Python looks inside the box and prints the content it finds. Notice variable names don't have quotes.
Variable names can't have spaces. A common practice is to use an underscore (
_) to separate words, which is a style called snake_case.
Cleaning Up Your Text
Often, the text you get isn't perfect for a prompt. It might have extra whitespace or inconsistent capitalization. Python gives us built-in tools called methods to clean up strings. A method is a function that belongs to a specific type of data, like a string. You call it by adding a dot and the method name to your variable.
Let's look at three useful string methods:
| Method | Action | Example |
|---|---|---|
.lower() | Converts the entire string to lowercase. | "Hello".lower() becomes "hello" |
.strip() | Removes any whitespace from the beginning and end. | " some text ".strip() becomes "some text" |
.replace(old, new) | Replaces every instance of one substring with another. | "I like cats.".replace("cats", "dogs") becomes "I like dogs." |
Imagine you have a user-submitted topic that's messy. You can chain these methods together to clean it in one go.
messy_topic = " Large Language Models "
# Clean it up
clean_topic = messy_topic.strip().lower()
print(clean_topic)
The output will be large language models, which is a much more consistent input for a prompt.
Building Prompts with F-Strings
Now, how do we combine our clean text with a standard prompt template? We use a special kind of string called an f-string. It lets you embed variables directly inside a string.
To make an f-string, just put the letter f right before the opening quote. Inside the string, you can place any variable name inside curly braces {}. Python will replace it with the variable's value.
topic = "the history of the printing press"
# This is our prompt template using an f-string
prompt = f"Write a one-paragraph summary about {topic}."
print(prompt)
The output is:
Write a one-paragraph summary about the history of the printing press.
This is the foundation of prompt engineering with code. You can programmatically insert any topic, user query, or piece of data into a well-structured prompt template.
Python's Rules and Errors
Like any language, Python has grammar rules. This grammar is called syntax. If you break a rule, the program won't run, and Python will give you an error message. Don't worry, errors are normal! They're just the computer's way of telling you it didn't understand your instructions.
For example, forgetting a closing quote on a string is a common mistake. This causes a SyntaxError, because the line is incomplete.
# This code will cause an error
print("This is an incomplete string)
Another common one is a NameError. This happens if you try to use a variable that you haven't defined yet.
# This code will also cause an error
print(my_variable)
If you see an error, read the message carefully. It usually tells you which line has the problem and gives a hint about what went wrong. Debugging is a core part of programming, and with practice, you'll learn to spot and fix these issues quickly.
What is the primary function in Python for displaying text on the screen?
In Python, a piece of text enclosed in quotes is called a _______.
You've now learned the basics of how Python handles text. You can create strings, store them in variables, clean them up with methods, and build dynamic prompts with f-strings. These are the fundamental building blocks for automating your work with language models.