A measurement is presented of the exclusive and semi-exclusive production of charged pion pairs in proton-proton collisions, pp to p(p*) + $π^+π^-$ + p(p*) here the $π^+π^-$ pair is emitted at central rapidities, and the scattered protons stay intact (p) or diffractively dissociate (p*) without detection. The measurement is performed with the CMS detector at the LHC, using a data sample corresponding to an integrated luminosity of 450 inverse microbarns collected at a center-of-mass energy of 7 TeV. The dipion cross section, measured for single-pion transverse momentum pt >0.2 GeV and rapidity abs(y)<2, is 26.5 +/- 0.3 (stat) +/- 5.0 (syst) +/- 1.1 (lumi) microbarns. The differential cross sections measured as a function of the invariant mass, pt, and y of the pion pair, and as a function of single-pion pt, are compared to phenomenological predictions.