Assessment of microbial viability plays a key role in human health protection. Optical imaging based on fluorescent dyes is a simple and convenient way to assess microbial viability. However, it is still a challenge to obtain stable, nontoxic and low-cost dyes. Herein, we prepared a nitrogen and phosphorus co-doped carbon nanodots (N, P-CDs) via a one-step solvothermal method. The prepared CDs possess plenty of functional groups and exhibit high stability, good biocompatibility, excellent photoluminescent and low toxicity. Especially, the properties of high quantum yield (89.9%) and highly negative surface charge (−41.9 mV) make the prepared N, P-CDs ideal materials for microbial differentiation. Compared with commercial dyes, our CDs are more stable, cost less, which can rapidly distinguish dead microorganisms from living ones with higher specificity.