If you want to remove the space only at the beginning of the data, then use YourData.trimstart()
But if the requirement is to eliminate space both at the beginning and end, use YourData.trim()
If it runs for one run it should work for all runs, have you checked is there a single space or multiple space in your input string? If it is multiple string replace method will replace only single space, and other spaces remains in your input