Punishment and forgiveness are not mutually exclusive. Those who don’t know God, fear punishment. Those who do, accept punishment at his hand (even welcome it) because they know he cares for them. If he punishes them, he punishes them for their good.
It’s a little bit like a small child crying because they are afraid of the needle. The more mature child does not cry because she knows the doctor is not trying to hurt her. She accepts the needle. In both instances the same amount of pain is experienced. “With acceptance comes peace.” Does this mean God will punish the repentant just as much as he will punish the unrepentant? Not at all.
“But the fear of God is the beginning of wisdom!”
Yes. But it is only the beginning. The person who only knows God is good fears him. The person who knows God is good and that he also loves them trusts him. “Perfect love casts out fear” (see 1 John 4:18).*
Those who are wise, trust God and live like Jesus did. They are honest, kind, and fair. They care about those God cares about; they help the poor and speak up for the oppressed. These people know God will punish them only if it is absolutely necessary.
“This is how love is made complete among us so that we will have confidence on the day of judgment: In this world we are like Jesus.” (1 John 4:17)
It is worth reading 1 John 4:17 in context. The obedient have nothing to fear. And the person who knows God but does fall into temptation, accepts punishment from God the way King David did. They do not shy from it.
The following explains how forgiveness and punishment can coexist.
*The more a person loves God, the less they fear him. The evil person does not fear God because they do not believe God is just (or they believe he does not exist). If there is no judgement day, there is nothing to fear (except other people). The good person fears God more than they fear people. That is why the Hebrew midwives disobeyed Pharaoh and did what was right (See Exodus 1:15-21). They feared God more than they feared Pharaoh. (I owe this insight about the midwives to Dennis Prager’s brilliant commentary on Exodus, The Rational Bible: Exodus. See here.)