Developing a non-toxic, cost-effective, biocompatible, selective, and sensitive optical sensor for the optical detection of 4-nitrophenol (4-NP) has been challenging yet significant. Among carbon-based materials, the selectivity of carbon dots toward 4-NP is a key area of research, and a comprehensive understanding is crucial to broaden its practical application. Our unique contribution to this field involves synthesizing and testing different N-doped carbon dots (CDs) for selective 4-NP detection through absorbance and photoluminescence. We have also theoretically provided the interaction mechanism between 4-NP and N-doped CDs (H-NCDs). The optimized hydrothermally synthesized nitrogen-doped CDs demonstrate excellent specificity towards 4-NP through simultaneous quenching of the PL (98 %) and redshift of the signal (34 nm) in an aqueous medium. The density functional theory (DFT) model that was performed to understand the electronic structure properties behind the interaction of 4-NP and CD could be a predictive tool for developing CD-based materials to enhance 4-NP capture. Our study provides a mechanistic understanding of the analyte-specific CD design. It offers promising implications for the practical application of our findings, thereby contributing a unique perspective to the field and benefiting various industries and environmental monitoring efforts, sparking optimism for the future.