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.