String.strip() in Python

后端 未结 5 1819
感情败类
感情败类 2021-01-30 03:07

While learning about python, I came upon this code, which takes a text file, splits each line into an array, and inserts it into a custom dictionary, where the array[0] is the k

5条回答
  •  后悔当初
    2021-01-30 03:47

    strip does nothing but, removes the the whitespace in your string. If you want to remove the extra whitepace from front and back of your string, you can use strip.

    The example string which can illustrate that is this:

    In [2]: x = "something \t like     \t this"
    In [4]: x.split('\t')
    Out[4]: ['something ', ' like     ', ' this']
    

    See, even after splitting with \t there is extra whitespace in first and second items which can be removed using strip in your code.

提交回复
热议问题