We investigated the associations of estimated free and total circulating testosterone and sex hormone-binding globulin (SHBG) with cancer risk in men and postmenopausal women, using a pan-cancer approach, including 19 cancers in UK Biobank.Risk was estimated using multivariable-adjusted Cox regression in up to 182,608 men and 122,112 postmenopausal women who were cancer-free at baseline. Participants diagnosed with cancer within two years of baseline were excluded. Hazard ratios (HRs) and confidence intervals (CIs) were corrected for regression dilution bias using repeat measurements. We accounted for multiple testing using the false discovery rate.In men, higher free testosterone was associated with higher risks of melanoma and prostate cancer (HR per 50 pmol/L increase=1.35, 95% CI 1.14-1.61 and 1.10,1.04-1.18, respectively). Higher total testosterone was associated with an elevated risk of liver cancer (HR per 5 nmol/L=2.45,1.56-3.84), and higher SHBG was associated with a higher risk of liver cancer (HR per 10 nmol/L=1.56,1.31-1.87) and a lower risk of prostate cancer (0.93,0.91-0.96); associations with liver cancer were attenuated after excluding early follow-up. In postmenopausal women, higher free and total testosterone and lower SHBG were associated with elevated risks of endometrial (HR per 10 pmol/L=1.59,1.32-1.90; HR per 0.5 nmol/L=1.34,1.18-1.52 and HR per 25 nmol/L=0.78,0.67-0.91, respectively) and breast cancer (1.32,1.22-1.43;1.24,1.17-1.31 and 0.88,0.83-0.94, respectively).We report a novel association of free testosterone with malignant melanoma in men; our findings also support known associations between sex hormones and risks for prostate, breast and endometrial cancers. The association with liver cancer in men may be attributable to reverse causation.