The photocatalytic hydrogen peroxide (H
2O
2) production by graphitic carbon nitride is a sustainable and environment-benign alternative approach of conventional anthraquinone autoxidation technology, but it is great challenges to promote two-electron O
2 reduction and water oxidation. Herein, we present the well-dispersed graphitic carbon nitride quantum dots decorated with cyano groups (Na-CNQD and K-CNQD) by thermal polymerization of melamine in the presence of metal fluoride. The quantum confinement and edge effect have endowed the photocatalysts with rich active sites, wide light absorption range and the inhibited charge recombination. The cyano moieties function as O
2 reduction centers to accept the photogenerated electrons and facilitate their rapid transfer to O
2 molecules. This process enables the selective two-electron reduction of O
2, leading to the production of H
2O
2. Concurrently, the valence band holes on the heptazine moiety oxidize water into H
2O
2. These synergistic effects promote photocatalytic H
2O
2 production from O
2 and H
2O without the need for additional photosensitizers, organic scavengers and co-catalysts. In contrast, pristine carbon nitride nanosheets remain inactive under the same conditions. This study offers new strategies for rational design of carbon-based materials for solar-to-chemical energy conversion.