A wave on a string is flipped when it reflects from a fixed end, and is reflected upright (no flip) when it reflects from a free end. Flipping the wave is equivalent to shifting it by half a wavelength.
Now consider a wave traveling along a light string tied to a heavy string. At the interface part of the wave is transmitted and part is reflected - the interface acts like a fixed end, so the reflected wave is inverted.
For a wave traveling along a heavy string tied to a light string the reflected part of the wave is not inverted when the wave reflects.
Light waves, or other EM waves, experience a 180o phase shift when reflecting from a higher-n medium (n2 > n1). This is equivalent to a shift of half a wavelength. Waves reflecting from a medium of lower index of refraction experience no phase shift.