The problem statement says that it is a "long cylindrical charge distribution ... of which a segment is shown". The end caps can be considered to be distant from the segment under consideration.
It's much like the uniform field assumption for the parallel plate capacitor, where the edge effects are taken to be negligible compared to the majority of the plate surface area.
If you're asking why the electric field lines don't spontaneously decide to bunch together into a tube shape and only run parallel to the cylinder, then take it as a property of space that it tries to "dilute" flux density -- it's as if the flux lines are mutually repellent, so they naturally arrange themselves into the minimum energy density state which is radial divergence.