RAW: No*, you can only replace spells of 1st level or higher
*Unless you use optional rules from Tasha's Cauldron of Everything, see down below.
Spells Known of 1st Level and Higher [...]
Additionally, when you gain a level in this class, you can choose one of the spells you know and replace it with another spell from the spell list, which also must be of a level for which you have spell slots.
That section only applies to spells of 1st level or higher (context matters)
For the sorcerer, ranger, bard, and warlock the sentence above is under the section Spells Known of 1st Level and Higher.
The phrase must be interpreted with that context. When read this way it is clear that when it says "one of the spells you know" it is implied that it means only those 1st level and higher. There is a cantrip section immediately above that one which does not have any such wording.
Both the old and the new spell must be ones that use spell slots (and cantrips don't)
Additionally, the phrasing implies that both the spell to be replaced and the one that it is being replaced with must use spell slots:
you can choose one of the spells you know and replace it with another
spell from the spell list, which also must be of a level for which you
have spell slots.
The implication being that the spell that is being replaced is already at a spell level for which you have spell slots. The reason they don't say this more explicitly is likely because of the context of it being under a section that is only talking about spells of 1st level or higher. And cantrips, despite being 0 level spells, don't use spell slots:
The first sentence of the rule on cantrips says they don't use spell slots. The rule isn't kidding.
Rules as Intended: Cantrips are not intended to be replaced
This also seems to follow designer guidance/intent as Jeremy Crawford says explicitly here:
The sorcerer's Spellcasting trait lets you replace a sorcerer spell
you know when you reach a new level in the class. The spell must be of
a level for which you have spell slots, which means it can't be a
cantrip; cantrips don't use spell slots.
Jeremy Crawford has also explained intent more generally:
Q: Is it possible at all to replace a known Cantrip with a different
one? Or is that a permanent choice?
A: Learning a cantrip is typically permanent. A DM could come up with
a story-based way to replace one w/ another.
Narrative reason: Cantrips are special
The narrative reason for this is likely because cantrips are different from spells in that:
Repeated practice has fixed the spell in the caster’s mind and infused the caster with the magic needed to produce the effect over and over.
Jeremy Crawford seems to support this narrative as well:
A cantrip is special: it grows in power as you do, and it is magically bound to you. In most D&D worlds, that bond isn't easy.
Tasha's optional rules
Tasha's Cauldron of Everything includes new optional class features that allow for the swapping of cantrips. The exact method varies from class to class. For example, the Wizard's Cantrip Formulas says:
You have scribed a set of arcane formulas in your spellbook that you can use to formulate a cantrip in your mind. Whenever you finish a long rest and consult these formulas in your spellbook, you can replace one wizard cantrip you know with another cantrip from the wizard spell list.
There isn't a provision in the rules to replace a cantrip once chosen
Also, from the lead dev, there is this statement of intent regarding the Eldritch Knight:
@OtterusMasterus: Eldritch Knights: when gaining a level, can they
exchange a cantrip for a spell for which they have spell slots?
@JeremyECrawford: The intent is that the Eldritch Knight replaces a spell of 1st level or higher with another non-cantrip spell.
The above is consistent with the general absence of any rules text in the PHB addressing the replacing of a cantrip as a character goes up in level. By that absence, by the lack of provision to replace any cantrip in any class upon leveling up, the general case is that once chosen a cantrip is yours forever. The "choosing 1st level spells and higher" feature has a specific way to change one's spell list as one goes up in level. Level 0 (cantrip) spells don't share that feature for any class.
This matches how a cantrip's power goes up with your character level. Let's look at fire bolt:
Make a ranged spell attack against the target. On a hit, the target takes 1d10 fire damage. A flammable object hit by this spell ignites if it isn't being worn or carried. This spell's damage increases by 1d10 when you reach 5th level (2d10), 11th level (3d10), and 17th level (4d10).
The concept is that you stick with a cantrip forever, and that it gets stronger as you (the character) get more powerful as you move into successively more challenging tiers of play. (p. 10, PHB; Adventure Tiers 2, 3, and 4 start at levels 5, 11, and 17).
If a character has levels in more than one class, do the character’s cantrips scale with character level or with the level in a spellcasting class?
Cantrips scale with character level. For example, a barbarian 2 / cleric 3 casts sacred flame as a 5th-level character. (Sage Advice Compendium, 2.0 p. 13)
Crawford also offers this advice in two different tweets. The choice is generally permanent:
Q: Is it possible at all to replace a known Cantrip with a different one? Or is that a permanent choice?
A: Learning a cantrip is typically permanent. A DM could come up with a story-based way to replace one w / another.
The cantrip is a unique kind of spell.
A cantrip is special: it grows in power as you do, and it is magically
bound to you. In most D&D worlds, that bond isn't easy.
In my first campaign, when some new material came out for Temple of Elemental Evil, my DM allowed me to replace one of the cleric cantrips I had chosen, Spare the Dying, with one that was in the EE Supplement: Thunderclap. The decision was thematic due to being a tempest cleric.
The Spellcasting feature does not allow for changing of cantrips, by the same justifications as outlined in the answers to the questions linked. Briefly:
The rule doesn't actually say that --- it's more along the lines of "if a cantrip could be replaced, this is what would happen". However, it should be noted that the statement refers to both cantrips and spells of level 1 or higher. The latter can be replaced as usual.