Single-walled carbon nanotubes (SWNTs) have strong potential for molecular electronics, owing to their unique structural and electronic properties. However, various outstanding issues still need to be resolved before SWNT-based devices can be made. In particular, large-scale, air-stable and controlled doping is highly desirable. Here we present a method for integrating organic molecules into SWNTs that promises to push the performance limit of these materials for molecular electronics. Reaction of SWNTs with molecules having large electron affinity and small ionization energy achieved p- and n-type doping, respectively. Optical characterization revealed that charge transfer between SWNTs and molecules starts at certain critical energies. X-ray diffraction experiments revealed that molecules are predominantly encapsulated inside SWNTs, resulting in an improved stability in air. The simplicity of the synthetic process offers a viable route for the large-scale production of SWNTs with controlled doping states.