By Gul Yusufzai and Javed Hussain

QUETTA/PARACHINAR, Pakistan (Reuters) - Bombs killed at least 40 people in Pakistan on Friday, with a suicide car bomber killing 13 in the southwestern city of Quetta, and two blasts later claiming at least 27 lives in the northwestern town of Parachinar, officials said.

A separate gun attack on police in the southern megacity of Karachi killed four officers on Friday evening, a security official said.

Seven police officers were among those killed in the first attack, in Quetta, which happened when police stopped the car to search it at a checkpoint.

Abdul Razzaq Cheema, director general of police in Baluchistan province, of which Quetta is capital, told Reuters the bomber had detonated a car packed with explosives.

At least 13 bodies were taken to hospital, along with 19 wounded people, said Wasim Baig, a spokesman for the Civil Hospital in Quetta. Nine security officials were among the wounded, said Fareed Sumalan, a doctor at the hospital.

Jamaat ur Ahrar, a splinter group of the Pakistani Taliban, claimed responsibility for the attack in a message sent to Reuters by its spokesman, Asad Mansur.

"Our attacks will continue until a true sharia system is enforced in Pakistan," the spokesman said, referring to Islamic law.

Islamic State (IS) also claimed responsibility for the attack in a message sent to journalists. Jamaat ur Ahrar and IS have jointly claimed responsibility for past attacks in Pakistan.

In the evening, several hundred kilometers to the northeast, two explosions in the town of Parachinar killed at least 27 people and wounded 120, a government official told Reuters.

The blasts were in a market and within three minutes of each other, senior government official Wazir Khan Wazir said. Parachinar is near the border with Afghanistan.

Many people were at the market buying food for iftar, the evening meal with which Muslims break the daily fast during the holy month of Ramadan. Ramadan ends this weekend.

Story continues

No group claimed responsibility for the Parachinar blasts.

POLICE THE TARGET?

In Baluchistan, provincial government spokesman Anwar ul Haq Kakar said the Quetta car bomb went off near the office of the inspector general (IG) of police.

"It's possible the IG office was the target, or the assailants were trying to enter the cantonment which is close by," he said, referring to an army housing area.

An official from Baluchistan's bomb disposal unit said the car had contained up to 95 kg of explosives.

Television footage showed emergency workers rushing to the debris-strewn scene as security officials cordoned it off.

Quetta is about 100 km (60 miles) east of the border with Afghanistan.

Resource-rich Baluchistan province has been plagued by violence for years. Separatist rebels are battling government forces while Taliban and other militant Islamist groups also operate there.

Baluchistan is also a main center of Chinese-backed "Belt and Road" infrastructure and energy projects involving some $57 billion worth of investment across Pakistan.

Militants loyal to the Islamic State group abducted and killed two Chinese nationals in Quetta last month.

That attack prompted Pakistan to boost security for Chinese nationals and other foreigners in the province, which is already one of the most militarized regions in the country.

Late on Friday, in Karachi, Pakistan's largest city and financial capital, four police were shot dead as they were observing the iftar meal to break the daily Ramadan fast, local TV and a security official said.

(Additional reporting by Saad Sayeed in Islamabad, Saud Mehsud in Dera Ismail Khan and Jibran Ahmed in Peshawar; Editing by Robert Birsel and Andrew Roche)