Nanostructured porous carbons, with its high specific surface area, rich pore structure, excellent conductivity and chemical stability, have become an excellent electrode material in advanced energy utilization technologies such as capacitive deionization and lithium–sulfur batteries. In this work, by controlling the concentration of oxidants and the addition of surfactants during the oxidative polymerization of pyrrole, the morphology and size of polypyrrole can be regulated. Nanostructured porous carbons with controllable morphology were successfully prepared by steam activation of polypyrrole particles and nanoribbons. In capacitive deionization experiment, the synthesized nanostructured carbon nanoribbon (NCNR) exhibits excellent electrochemical properties due to their rich pore structure and large surface area (1258 m2 g–1). In a 500 mg L–1 NaCl solution, it has an electrosorption capacity of 12.9 mg g–1 at 1.2 V. In addition, when NCNR is used as a host material for sulfur in lithium–sulfur batteries, it exhibits significantly improved discharge capacity and excellent cycling stability (maintaining a capacity of 672 mA h g–1 after 200 cycles at a rate of 0.5 C), providing new ideas for solving the problems of capacity degradation faced by lithium–sulfur batteries.