> Source URL: /resources/print-statements.resource
# Print Statements

## What It Is

The `print()` function displays output to the screen. Whatever you put inside the parentheses will appear in the terminal when you run your code.

## Why We Use It

Print statements let you:

- **See results** of your code
- **Display information** to users
- **Debug problems** by showing what's happening at each step
- **Create formatted output** like stat cards, reports, or leaderboards

Every sports graphic you see on TV — scores, player stats, standings — is essentially printed output, just with fancier styling.

---

## How to Use It

### Basic Print

```python
print("Hello, sports fans!")
```

**Output:**

```
Hello, sports fans!
```

### Printing Variables

```python
player = "Simone Biles"
medals = 37

print(player)
print(medals)
```

**Output:**

```
Simone Biles
37
```

### Combining Text and Variables with F-Strings

F-strings (formatted strings) let you mix text and variables easily. Put an `f` before the quotes, and wrap variable names in curly braces `{}`:

```python
player = "Simone Biles"
medals = 37

print(f"{player} has won {medals} World and Olympic medals!")
```

**Output:**

```
Simone Biles has won 37 World and Olympic medals!
```

---

## Multiple Print Statements

Each `print()` creates a new line:

```python
print("=== STAT CARD ===")
print("Player: Wilt Chamberlain")
print("Points: 100")
print("Date: March 2, 1962")
print("=================")
```

**Output:**

```
=== STAT CARD ===
Player: Wilt Chamberlain
Points: 100
Date: March 2, 1962
=================
```

### Blank Lines

Use `print()` with nothing inside to create a blank line:

```python
print("First section")
print()
print("Second section (with space above)")
```

**Output:**

```
First section

Second section (with space above)
```

---

## Formatting Tips

### Building a Stat Card

```python
player = "Joe DiMaggio"
achievement = "56-game hitting streak"
year = 1941
team = "New York Yankees"

print("╔══════════════════════════════════════╗")
print(f"  {player}")
print(f"  {team}")
print("──────────────────────────────────────")
print(f"  Achievement: {achievement}")
print(f"  Year: {year}")
print("╚══════════════════════════════════════╝")
```

**Output:**

```
╔══════════════════════════════════════╗
  Joe DiMaggio
  New York Yankees
──────────────────────────────────────
  Achievement: 56-game hitting streak
  Year: 1941
╚══════════════════════════════════════╝
```

### Using Special Characters

You can use symbols to make your output look better:

```python
print("★ MVP Season Stats ★")
print("━" * 30)  # Prints the ━ character 30 times
print("Points: 2,818")
```

**Output:**

```
★ MVP Season Stats ★
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Points: 2,818
```

---

## Common Patterns

### Print with calculation

You can do math right inside the curly braces `{}` of an f-string. Python calculates the result and shows it in your output. Here are the basic math operators you can use:

- `+` for addition
- `-` for subtraction
- `*` for multiplication
- `/` for division

**Calculating points per game:**

```python
points = 100
games = 1

print(f"Points per game: {points / games}")
```

**Output:**

```
Points per game: 100.0
```

**Calculating win percentage:**

```python
wins = 12
total_games = 16

print(f"Win percentage: {wins / total_games * 100}%")
```

**Output:**

```
Win percentage: 75.0%
```

**Adding up total points:**

```python
touchdowns = 3
field_goals = 2

print(f"Total points: {touchdowns * 6 + field_goals * 3}")
```

**Output:**

```
Total points: 24
```

**Calculating batting average:**

```python
hits = 45
at_bats = 150

print(f"Batting average: {hits / at_bats:.3f}") #.3f formats the number with 3 digits after the decimal points
```

**Output:**

```
Batting average: 0.300
```

### Print multiple values

```python
player = "Lionel Messi"
goals = 8
tournament = "2022 World Cup"

print(player, "-", goals, "goals at", tournament)
```

**Output:**

```
Lionel Messi - 8 goals at 2022 World Cup
```

---

## Common Pitfalls

| Problem                                   | What Went Wrong                  | Fix                                    |
| ----------------------------------------- | -------------------------------- | -------------------------------------- |
| `SyntaxError: unterminated string`        | Forgot closing quote             | Add the missing `"` at the end         |
| `NameError: name 'player' is not defined` | Variable doesn't exist           | Create the variable before printing it |
| Curly braces showing literally `{player}` | Forgot the `f` before the string | Change `"{player}"` to `f"{player}"`   |
| Extra spaces or weird formatting          | Spaces inside your string        | Check what's between your quotes       |

---

## Quick Reference

```python
# Basic print
print("Hello!")

# Print a variable
print(player_name)

# F-string (text + variables)
print(f"{player} scored {points} points")

# Multiple values with commas
print("Score:", home, "-", away)

# Blank line
print()

# Repeat a character
print("=" * 20)  # Prints: ====================
```


---

## Backlinks

The following sources link to this document:

- [Print Statements](/resources/index.resources.llm.md)
- [Print Statements](/users/annie-h/week-5.path.llm.md)
- [Print Statements](/users/charlie-m/week-5.path.llm.md)
- [Print Statements](/users/emily-kate-s/week-5.path.llm.md)
- [Print Statements](/users/josiah-c/week-5.path.llm.md)
- [Print Statements](/users/john-p/week-5.path.llm.md)
- [Print Statements](/users/jake-f/week-5.path.llm.md)
- [Print Statements](/users/elizabeth-b/week-5.path.llm.md)
