I\'m building a website with Python/Django. Users submit tags. Each tag can contain multiple words. Each tag has an ID number. I want to make sure tags that are formatted sl
"electric guitar".split()
will give you ['electric', 'guitar']
. So will "electric \tguitar"
.
This function removes everything which is not digit in a string. I use it all over the place.
def parseInt(string):
if isinstance(string, (str, int, unicode)):
try:
digit = int(''.join([x for x in string if x.isdigit() ]))
except ValueError:
return False
else:
return digit
else:
return False
I would use Django's slugify
method, which condenses spaces into a single dash and other helpful features:
from django.template.defaultfilters import slugify
Split on any whitespace, then join on a single space.
' '.join(s.split())
There could be many white spaces like below:
var = " This is the example of how to remove spaces "
Just do simple task like, use replace function:
realVar = var.replace(" ",'')
Now the outpur would be:
Thisistheexampleofhowtoremovespaces
Just Chill......... :-)
>>> import re
>>> re.sub(r'\s+', ' ', 'some test with ugly whitespace')
'some test with ugly whitespace'