Python Use Strip() To Remove White Space

Some time you deal with blank space something like this '          10.255.1.10/24    '  if you deal with this some time you need to remove white space. The easy way you can use strip() to remove that space.

Before use stript() we can see the space
>>> ipaddr='          10.255.1.10/24    '
>>> print ipaddr
          10.255.1.10/24

After using strip()
>>> ipaddr.strip()
'10.255.1.10/24'


this is very usefull when you wanna use startswith() or endswith(). because those won't work when a space inside it. let's try :

>>> ipaddr
'          10.255.1.10/24    '
>>>
>>> ipaddr.startswith('10')
False
>>>
see that!

Now we try to use strip() first we just create a variable for new data :
>>> ipaddrstrip = ipaddr.strip()
>>> ipaddrstrip
'10.255.1.10/24'
>>> ipaddrstrip.startswith('10')
True

it's work!

Tulis komentar anda... Conversion Conversion Emoticon Emoticon

Thanks for your comment