Mr Obama has told an interfaith memorial service the violence threatens to expose the deepest fault lines in US democracy.
Five officers were killed and a number of others were injured when gunman Micah Johnson opened fire at a protest against police violence.
There were five empty seats at the memorial service in Dallas for the five officers killed.
The White House said President Barack Obama worked late into the night writing his speech.
It is the11th time he has had to console a community in mourning after a mass shooting.
News that makes sense
Your trusted source for staying up-to-date with the world around you. Get free daily news updates and analysis, straight to your inbox.
"I know that Americans are struggling right now with what we've witnessed over the past week. First, the shootings in Minnesota and Baton Rouge, the protests, then the targeting of police by the shooter here, an act not just of demented violence but of racial hatred. All of it's left us wounded and angry and hurt. It's as if the deepest fault lines of our democracy have suddenly been exposed, perhaps even widened."
His emotional speech balanced praise for the work of police officers with criticism of racial discrimination.
"As a society, we choose to under-invest in decent schools. We allow poverty to fester so that entire neighbourhoods offer no prospect for gainful employment. We refuse to fund drug-treatment and mental-health programs. (applause ... ) We flood communities with so many guns that it is easier for a teenager to buy a glock than get his hands on a computer or even a book. (applause ...) And then we tell the police, 'You're a social worker. You're the parent. You're the teacher. You're the drug counsellor.'"
Former US president George W Bush, a resident of Dallas, spoke of his heartbreak.
"Too often, we judge other groups by their worst examples while judging ourselves by our best intentions."
But he spoke of a common bond in the United States.
"Americans, I think, have a great advantage. To renew our unity, we only need to remember our values. We have never been held together by blood or background. We are bound by things of the spirit, by shared commitments to common ideals."
Earlier, thousands attended a candlelight vigil outside the Dallas police department.
Genevieve Juarez, who was at the vigil, says she can already feel a positive change.
"I mean, you can feel it. As soon as it happened on Thursday, you can just feel, all of a sudden, the support and everybody coming together and just, I don't know, a change of emotion, a change of everything. I know it's going to get better."
The partner of one of the slain officers, Mike Smith, called on citizens to use him as an example to make the city a better place.
"I and those who loved Mike the most choose to honour his legacy by choosing -- because it is a choice -- not to let our anger drag us into a darker place. But, instead, we choose to continue Mike's fight for good and to not let the evil prevail."

