I have a list of objects appended from a mysql database and contain spaces. I wish to remove the spaces such as below, but the code im using doesnt work?
hello = ['999 ',' 666 ']
k = []
for i in hello:
str(i).replace(' ','')
k.append(i)
print k
Strings in Python are immutable (meaning that their data cannot be modified) so the replace method doesn’t modify the string – it returns a new string. You could fix your code as follows:
However a better way to achieve your aim is to use a list comprehension. For example the following code removes leading and trailing spaces from every string in the list using
strip: