If this is a hobby project and you have an optocoupler lying around, it might make sense, assuming your optcoupler is any better at withstanding ESD strikes than your uC (Those thousand-volt withstand ratings on optos are for voltage differences between the transmit and receive side, not between the two terminals on the same side of the isolation barrier).
But if you have to buy the parts, a couple of Schottky diodes is probably cheaper:
simulate this circuit – Schematic created using CircuitLab
Your micro probably already has a similar structure internally, but it's ability to absorb transients is limited to avoid slowing down the input. If you expect only slow inputs, you can provide additional, beefier external diodes to improve the ESD performance. (Note: CircuitLab doesn't seem to have a Schottky symbol, but be sure to use Schottky diodes, otherwise the internal diodes on the uC will end up taking up most of the current before your external diodes start to work)
Edit: Yes, this will also work for outputs.