The influence of the nuclear medium on the production of charged hadrons in semi-inclusive deep-inelastic scattering has been studied by the HERMES experiment at DESY using a 27.5 GeV positron beam. The differential multiplicity of charged hadrons and identified charged pions from nitrogen relative to that from deuterium has been measured as a function of the virtual photon energy \nu and the fraction z of this energy transferred to the hadron. There are observed substantial reductions of the multiplicity ratio R_M^h at low \nu and at high z, both of which are well described by a gluon-bremsstrahlung model of hadronization. A significant difference of the \nu-dependence of R_M^h is found between positive and negative hadrons. This is interpreted in terms of a difference between the formation times of protons and pions, using a phenomenological model to describe the \nu- and z-dependence of R_M^h.