Variables and Strings
15 min In ProgressLearning Objectives
- Understand what a variable is and how to name it
- Create and manipulate string values
- Use the print() function to output data
What is a Variable?
A variable is a named container that stores a value. In Python, you create a variable by assigning a value with the = operator.
name = "Alice"
age = 30
print(name)
print(age)
Working with Strings
Strings are text values surrounded by single or double quotes. You can combine strings with + and repeat them with *.
greeting = "Hello"
name = "World"
message = greeting + ", " + name + "!"
print(message)
print("Ha" * 3)
String Formatting with f-strings
f-strings let you embed expressions inside string literals using curly braces.
first_name = "Ada"
last_name = "Lovelace"
print(f"My name is {first_name} {last_name}")
Course Example: Name Tag Generator
This script from your course uses command-line arguments to build a simple name tag.
When running this example, provide first and last names as arguments.
"""
This module is a name tag generator.
"""
import sys
print("*---------------------------------")
print("|", "First name: ", sys.argv[1])
print("|", "Second name: ", sys.argv[2])
print("*---------------------------------")
📝 Exercise: Create Your Own Bio
Write a script that creates variables for your name, city, and hobby, then prints a sentence using an f-string.
code.py
Output