Stitching Words and Numbers Together
Okay so here's the thing. You know how to make words. You know how to make numbers. Now you're going to try to put them in the same sentence, and Python is going to be weirdly picky about it.
Here's what I mean. You've probably typed something like this already:
``python age = 40 print("I am " + age + " years old") ``
Run that and Python throws an error at you. Something like can only concatenate str (not "int") to str. Which, fair, it's telling you exactly what's wrong, it's just using computer words to do it.
Here's the plain-language version of what happened. + means two different things to Python depending on what's on either side of it. Between two numbers, + means add them. Between two words (strings, if you want the real term), + means stick them together. But Python won't let you mix a number and a word with + because it genuinely doesn't know if you mean "add these" or "smoosh these together." So it just stops and asks you to be clearer. Which, honestly, is a pretty reasonable thing to ask.
The fix: str()
The fix is to tell Python "hey, treat this number like a word for a second." You do that with str(), which just means "string-ify this."
``python age = 40 print("I am " + str(age) + " years old") ``
That's it. That's the whole trick. str(age) takes your number and wraps it up so it looks like text to Python, and then the + can do its "stick these words together" job without getting confused.
Type that in yourself, don't copy-paste it. I say this every lesson and I'll keep saying it, your fingers remember stuff your eyes skim right past. You'll make a typo, you'll fix it, and that fixing is where the learning actually happens.
A super common mistake
People forget the parentheses, or forget which variable needs wrapping, or wrap all of them out of habit when only one needed it. All of that is fine and normal. Here's a broken version and the fixed version side by side so you can see the difference:
```python # broken name = "Taylor" years = 12 print("I've been shooting for " + name + " for " + years + " years")
fixed print("I've been shooting for " + name + " for " + str(years) + " years") ```
Notice name didn't need str() because it was already a word. Only years needed it, because years is a number pretending to be part of a sentence.
Try this at home
Open up whatever free editor you've been using (still free, still enough, I will die on this hill) and build yourself a tiny "about me" line using at least one word variable and one number variable stitched together with + and str(). Something like:
``python city = "American Fork" elevation = 4600 print("I live in " + city + ", which sits at about " + str(elevation) + " feet.") ``
Run it. Break it on purpose, take the str() back out, watch the error message show up, put it back. Getting comfortable reading that error is honestly half the skill. It's not scary, it's just Python being specific.
When I first figured this stuff out
I mentioned a while back that I taught myself loops by writing a thing to spit out every possible starting move in a board game I was obsessed with, filled half a moleskin notebook with output nobody asked for. Same project is where I first ran into this exact problem, actually. I wanted the output to read like "Move 1: rook to..." instead of just a wall of numbers, and I kept trying to jam the move number straight into the sentence with + and Python kept yelling at me. Took me embarrassingly long to realize I just needed str() around the number. Once it clicked though, it clicked hard, and I went back and made all my output readable like actual sentences instead of a spreadsheet dump. Small fix, big difference in how it felt to look at.
One opinion, since we're here
You do not need to be good at math to do any of this. People hear "numbers and words together" and think it's some algebra thing. It's not. It's bookkeeping. You're just telling Python which drawer to put things in, string drawer or number drawer, so it knows how to handle them. That's the whole skill. Stubbornness gets you further than math ever will in this class.
Before next time
Take one of your earlier programs, the grocery total one works great for this, and rewrite the final print() line so it reads like a real sentence with str() doing the stitching. If it breaks, good, that's the interesting part.