President Barack Obama has urged Americans not to see the country as being riven into opposing groups after an attack that killed five policemen in Dallas and the high-profile police shootings of two black men in Minnesota and Louisiana.
"First of all, as painful as this week has been, I firmly believe that America is not as divided as some have suggested," Obama said on Saturday.
"Americans of all races and all backgrounds are rightly outraged by the inexcusable attacks on police, whether it's in Dallas or any place else," he said.
Authorities have named former US Army reservist Micah Johnson as the lone gunman in Thursday night's attack in Dallas, which came at the end of a rally to protest against police killings.
Related reading
Dallas sniper was army vet, loner
They said he had embraced militant black nationalism and expressed anger over police shootings and a desire to "kill white people, especially white officers."
"The demented individual who carried out those attacks in Dallas, he's no more representative of African-Americans than the shooter in Charleston was representative of white Americans or the shooter in Orlando or San Bernardino were representative of Muslim-Americans," Obama said, referring to a string of mass shootings in the past year.
The attack in Dallas also wounded seven other officers and two civilians.
Johnson, 25, was killed by a bomb-carrying robot deployed against him in a parking garage where he had holed up and refused to surrender during hours of negotiations with police, authorities said on Friday.
Obama, while saying there was a "persistent problem of African-Americans and Latinos being treated differently in our criminal justice system," stressed he did not believe the country was descending into the polarisation seen in the sometimes violent civil rights struggles of the 1960s.
The rally in Dallas followed the fatal police shootings of Philando Castile, 32, near St. Paul, Minnesota, on Wednesday, and Alton Sterling, 37, in Baton Rouge, Louisiana, on Tuesday.
Related reading
Man wrongly identified as Dallas shooter speaks out
Obama reprised a frustration he frequently expresses over lax gun laws in the US, saying the country is unique among advanced countries in the scale of violence experienced.
Referring to the death of Castile, after a traffic stop, Obama said "we don't know yet what happened, but we do know that there was a gun in the car that apparently was licensed but it caused, in some fashion, those tragic events."
Thursday's attack did not stop demonstrations around the country against police killings.
Early on Saturday, up to 30 protesters were arrested near police headquarters in Baton Rouge.
Tensions rose on both sides after an officer pulled a weapon during the demonstration. No shots were fired and Baton Rouge police said they were reviewing the incident.
Police use of force, particularly against African-Americans, has come under intense and sometimes angry scrutiny in the past two years because of a string of high-profile deaths in cities from Ferguson, Missouri, to New York.
Investigators have concluded that Johnson, armed with a rifle, was the lone gunman, and did not have links to any international militant group.
A search of the gunman's home just outside Dallas found bomb-making materials, ballistic vests, rifles, ammunition and a personal journal of combat tactics, though he had no previous criminal history, police said.
Police said social media entries showed he subscribed to a militant black nationalist ideology, including an anti-white diatribe posted last week on a Facebook page of a group called the Black Panther Party Mississippi.
The death toll in Dallas was the highest for US police in the line of duty from a single event since the September 11, 2001, attacks on New York and Washington.