Resolve Python IndexError: string index out of range during string concatenation by fixing loop bounds and using safe iteration methods.