President Recep Tayyip Erdogan has been repeatedly returned to office in elections generally seen as fair, but he has had to battle enemies on many fronts to maintain and consolidate his grip on power.
Even adversaries who seemed defeated or contained have come back to strike again. Whether with the military, the Kurdish minority or social media, Erdogan has fought some battles multiple times.
Erdogan has said the coup attempt was a "blessing from God" to enable him to clean his enemies out of the state.
Fethullah Gulen: Currently, enemy number one.
The Turkish-born US-based Islamic preacher was once an ally of Erdogan. His followers filled the ranks of the civil service in Turkey, especially the judiciary and law enforcement. The two men worked together to weaken the old military elite's control within the state.
However, in recent years a schism emerged for reasons that were never made public.
In 2013, a wide-ranging corruption investigation was opened against top officials, threatening the stability of the government. Erdogan, blaming the Gulenists for launching the probe, cracked down, purging many thousands from the police and judiciary.
Since then, not a week goes by without the announcement of numerous arrests of alleged Gulenists from the "parallel state." The government claims - without proof - that this group's "fingerprints" are all over the coup attempt.
Erdogan worked with the Gulenists to weaken the power of the military, which has unseated four governments, amid signs that leftovers from the generals' so-called deep state had machinations to control the country.
In 2007, an investigation was officially opened into Ergenekon, an alleged clandestine secularist, nationalist and military-oriented organization. Hundreds were arrested in the following years, from generals to journalists, accused of trying to foment unrest and potentially unseat the government.
Ultimately, nearly all those accused of involvement were released and the evidence against them found to be doctored.
When the coup attempt was under way, Erdogan used Facetime - a phone application - to call into television stations and urge his supporters to take to the streets. It was a noteworthy move by a president who has repeatedly expressed his disdain for social media.
In 2014, after alleged Gulenists released wiretapped phone conversations purporting to show corruption within the government, Twitter and YouTube were banned, as Erdogan threatened to "wipe out" such platforms. But users found workarounds and ultimately the bans were lifted.
After terrorist attacks - increasingly common in recent months - the government throttles social media sites.
The 1980 military coup had disastrous consequences for the country with a wide-ranging crackdown. Among the victims was the Kurdish language, which was banned for a number of years.
In 1984, the Kurdistan Workers' Party (PKK) emerged as an armed guerilla movement. The group has in recent years moderated its approach, focused now on autonomy rather than separatism.
In 2013, Erdogan and the PKK reached a ceasefire and peace talks were undertaken. In 2015, government ministers and Kurdish leaders outlined the so-called Dolmabahce Agreement, a road map to take the process forward. Erdogan, for reasons unclear, rejected the deal.
In June that year, the pro-Kurdish Peoples' Democratic Party (HDP) was elected to parliament for the first time, seen as a step towards political inclusion, but also denying Erdogan's ruling Justice and Development Party (AKP) a majority.
In July, a suicide bombing targeted a pro-Kurdish group, and the PKK killed two police officers, alleging - without proof - that the policemen were involved. A fresh cycle of violence has since dominated the country, in part being fought by some of the generals accused of involvement in the coup attempt.
Erdogan has also marked the HDP as an enemy.
Since 2015, Islamic State has targeted Turkey on a number of occasions with suicide bombings and rocket attacks from Syria. Critics say the government did not take the threat from the group seriously enough at first, as it was focused on trying to overthrow Syrian President Bashar al-Assad and worried about Kurdish gains in northern Syria.