Toddlers are parrots.
They all go through this.
The problem isn't "how to get him to stop it."
The way to handle it is: just be honest. So that, the child learns about it, and that adults make mistakes too.
You just tell the child. Directly.
Tell him it is a naughty word. YOU apologize for saying it and tell him you made a mistake. That you try your best. Then you tell him to do the same.
It is an "adult" word, not for children.
Then you TELL him, that for frustrations or whatnot, to make up "funny" names for it. This is what I tell my kids who are 5 and 9.
They say things like "Oh popcorn!" or "Fuzz brain" or "Shazbot" etc.
Whatever works and is funny than the typical curse words.
I have said the "F" word myself to myself, when irked.
My son LATER... like weeks later, said it too when he was mad. I asked him why he said that word and he said "Because you said that before..." So then I simply apologized, told him I was wrong and Mommy made a mistake and I should have used a different word, and he did know via common sense, that it is a naughty word.
No biggie.
At least, my kids don't swear in school!
The thing is, kids WILL learn what words are bad words. They may experiment with it for a little bit. But they know, because you teach them. But, when adults say it... be prepared for the child to correct YOU. That is what my kids do. If/when I mistakenly swear... under my breath... when I am irked.