CAPTURING the essence of a country's psyche in fiction is a challenge, and many a writer has returned empty-handed from a hunting trip into the woods. But if you paid attention in school you'll know that Robert Louis Stevenson and James Hogg are the authors considered to have done it most successfully. In The Strange Case Of Dr Jekyll And Mr Hyde and The Private Memoirs And Confessions Of A Justified Sinner respectively, Stevenson and Hogg serve up memorable fictional ciphers for a people in whom the moral, the staid and the upright battle constantly with the unruly, the violent and the uncanny. That's us, by the way.

Among the cast of characters Irvine Welsh unveiled in his 1993 novel Trainspotting, violence and unruliness win out almost every time. Anything that is staid or upright in their personalities they reject. Here are their names again, as if you could ever forget them: Renton, Spud, Sick Boy, Begbie.

The first three of that parcel of rogues are drug addicts infected with varying degrees of cynicism about their home town of Edinburgh, about work and, in particular, about Scotland and their place in it. The fourth is an unalloyed sociopath wound tight as a razor-sharp spring who, if he had his way, would burn every book in existence.

But through those characters, Welsh does as much as Stevenson and Hogg to nail what it is to be Scottish. Of course there's the much-quoted “It's sh**e being Scottish” sequence, words spoken by Renton during an ill-advised trip out of the city and rendered in the 1996 film adaptation pretty much as they are in the novel, right down to the “colonised by w***ers” bit. (Another memory-jogger for you: the Scots, says Renton, are “the lowest of the low, the scum of the earth. The most wretched, servile, miserable, pathetic trash that was ever shat intae creation”.)

But there's more to Welsh's take on Scotland and Scottishness than just that. Writing ahead of the release of T2, the much-anticipated Trainspotting sequel which has its world premiere in Edinburgh tonight, the author steps back a little from the characters he first created a quarter of a century ago and takes a more nuanced overview of them – and, by extension, of the Scottish qualities they represent.

Renton and Sick Boy he sees as being in a “powerful symbiotic relationship … the characters both want to be each other. Simon [Sick Boy] wants to be as intelligent and detached as Renton. For his part Renton wants to be as passionate and successful as Simon”. Spud, meanwhile, is viewed by his creator as “a loveable loser” while Begbie is “just this crazy force. And in T2 he’s not rehabilitated at all. There is still this force of rage”.

“They are constantly trying to be better,” Welsh continues. “No matter how dark the situation, the audience will forgive them as long as they’re trying to make an effort to reach for the light switch and get their way out.”

Intellect and passion in opposition. Aggression, hope, self-hatred and contradiction. Fortitude mixed with a sense of victimhood. What's all that if it isn't a recipe for being Scottish?

But go deeper into the wider world of Trainspotting – the stage adaptation and its recent revival, the first film and especially the sequel – and there are more insights to be had. Because as well as seeing in its leads a four-sided reflection of the competing character traits of the Scottish psyche, we can map onto their journey some of Scotland's own recent history. After all, T2 is essentially a film about growing up, growing older and learning to deal with the inevitable feelings of loss and regret that accompany the ageing process, as well as the wisdom and experience it can bring – experience which, in the case of Begbie in T2, includes a stint in prison. And that's a journey that post-devolution Scotland should be able to relate to.

Like the original Trainspotting film, which provided a shot in the arm (sorry) for a British film industry still mired in costume dramas and Home Counties rom-coms, Scotland in 1996 was a country looking to shake things up. And, irony of ironies, while the film was the very opposite of misty-eyed and sentimental on the subject of nationhood, in a way its success went hand in hand with the country's increasing sense of the same.

A little over a year after Trainspotting was released, Tony Blair's Labour government came to power promising a referendum on Scottish devolution. This was duly delivered in September 1997, followed a year later by the 1998 Scotland Act and, on May 12, 1999, by Winnie Ewing speaking these words: “The Scottish Parliament, adjourned on the 25th day of March in the year 1707, is hereby reconvened.”

It was, says Trainspotting and T2 producer Andrew Macdonald, a time of dynamism and optimism for filmmakers, actors and for Scots in general.

“Nineteen ninety-six was a good time to be Scottish," says Macdonald. "And everywhere you went in the world, it was suddenly cool to be a Scot. It helped that we had such a talented local cast ... It’s hard to imagine a more talented group of people from such a small country. So much has happened politically since then, so it is going to be interesting to see how people react to the uniquely Scottish feel of T2.”

The plot of T2 is based partly on Porno, the 10-years-on sequel Welsh published in 2002, and on a new script by original Trainspotting screenwriter John Hodge. It finds Renton returning to the capital 20 years after he ripped off his friends by stealing the proceeds of an ill-gotten financial windfall they had promised to share. Sick Boy is running a pub and indulging in a little light blackmail, Begbie has just come out of prison and Spud is, well, being Spud.

Renton discovers a city much changed and much gentrified, and returning director Danny Boyle makes exploring that change one of the themes of T2. It's not hard to see why: it was only when he, Welsh and Hodge convened in the capital in late 2014 to try to iron out the problems which were making the sequel look increasingly unlikely that they were able to make the breakthrough they needed. It was as if they needed to see – or, more likely, be shown by Welsh – how Edinburgh looked and felt 20 years on, and to let the city re-energise them.

For Boyle, that resulted in him making a film which is as much about Edinburgh as it is a film set there, and it meant filming almost entirely in the capital when the first film had been shot mostly in Glasgow. It also required some recalibration from Glasgow-born Hodge.

“Edinburgh is so much busier [now],” he says. “[In 1996] we didn’t stop the traffic, we didn’t even stop the pedestrians … and the east end of Glasgow has changed beyond recognition. Huge council estates where we shot have all been levelled and a lot of pubs have shut down.”

Again, this is part of Scotland's recent story. Since becoming a de facto European capital, Edinburgh has seen its population rocket as Glasgow's has shrunk. Demographers project a 120,000 increase in population over the next 20 years, meaning the city will be bigger than Glasgow by 2037. The capital is already the most prosperous city outside London (with house prices to match) and the percentage of the workforce with a degree-level education is higher than anywhere else in the UK, including London. It regularly tops quality of life and Best Place To Live lists.

Of course nobody would mistake Renton, Spud, Sick Boy and Begbie for members of the metropolitan elite and no quality of life index would be richer for their inclusion. A degree-level education is not something any of them ever aspired to either. In fact it's a moot point whether they should all even be alive: Scottish mortality rates are hardly the envy of the world and levels of drug-related deaths, always high, actually doubled between 2006 and 2014. The average age of death for drug victims is 40, probably slightly younger than the age of the T2 characters. That they're all still standing is an achievement under the circumstances.

So in another way, mapping Scotland's political and cultural journey onto the Trainspotting characters is a chastening experience: they're no less venal, amoral or prone to violence than before, they're just more knackered-looking than before.

“You see their faces, and it’s immediate,” says Danny Boyle. “There’s a pathos. It’s to do with our awareness of what time has done to them, and to us. The film kind of telescopes time. You look one way and the past is there, so close. You look again, and it’s gone.”

Jonny Lee Miller, the English actor who plays Sick Boy, agrees.

“I always said there was no point in making a sequel to Trainspotting unless you’re examining some bigger issues,” he says. “What’s it like being older? What have you done? What’s happened to the characters and what are the implications? A straightforward sequel to a caper, with the answers to who got away and who got revenge, becomes very boring really. The only way you could make it interesting is to put people’s lives in between it.”

But as much as T2 is about time and its effects on those lives, so is it about masculinity, something Boyle only realised when he sat with editor Jon Harris watching an early cut of the finished film.

“It’s the difference between boyhood and manhood. The irresponsibility of boyhood is the first movie: you don't give a f*** about anything, least of all time. Or rather, you don't think you do. T2 flips it. It’s time that doesn't give a f*** about you. Period. The film is saturated with images of disappointed men, and disappointed women, and disappointed children, even imaginary ones … Their bravado was so effortless before, but now it struggles to revive itself, so no wonder they seek each other out and endlessly try to recreate the past, either to enjoy it or take revenge on it.”

You can stretch a comparison too far, of course. But is that also a description of Scotland in 2017: one half of the country still disappointed by the outcome of the 2014 independence referendum, the other half's feeling of satisfaction dampened by Brexit and Trump, the dreams around the gleaming new parliament starting to tarnish a little – and still no prospect of ever seeing another goal like the one Archie Gemmill scored against Holland in 1978?

Scotland may have grown up with Trainspotting, and be able to see in its characters' journey a reflection of its own. But it remains to be seen if the mirror flatters or appals.

T2 Trainspotting is released on Friday