A first-principles approach is used to establish that substitutional phosphorus atoms within carbon nanotubes strongly modify the chemical properties of the surface, thus creating highly localized sites with specific affinity towards acceptor molecules. Phosphorus–nitrogen co-dopants within the tubes have a similar effect for acceptor molecules, but the P–N bond can also accept charge, resulting in affinity towards donor molecules. This molecular selectivity is illustrated in CO and NH3 adsorbed on PN-doped nanotubes, O2 on P-doped nanotubes, and NO2 and SO2 on both P- and PN-doped nanotubes. The adsorption of different chemical species onto the doped nanotubes modifies the dopant-induced localized states, which subsequently alter the electronic conductance. Although SO2 and CO adsorptions cause minor shifts in electronic conductance, NH3, NO2, and O2adsorptions induce the suppression of a conductance dip. Conversely, the adsorption of NO2 on PN-doped nanotubes is accompanied with the appearance of an additional dip in conductance, correlated with a shift of the existing ones. Overall these changes in electric conductance provide an efficient way to detect selectively the presence of specific molecules. Additionally, the high oxidation potential of the P-doped nanotubes makes them good candidates for electrode materials in hydrogen fuel cells.