Or take the historic F-line streetcar on Market from 8th to 4th -- just $2 and a fun experience!
I walk down 5th between Market and Mission five times a week, and I've only had a problem once (and that was in broad daylight -- crazy homeless guy got in my face a few weeks ago and tried to punch me, so that was fun). By all means take 4th if you prefer, but 5th is fine; still, walk on the east side (by the mall) to be safer.