String.strip() in Python

后端 未结 5 1822
感情败类
感情败类 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:53

    No, it is better practice to leave them out.

    Without strip(), you can have empty keys and values:

    applesround, fruity things
    orangesround, fruity things
    bananas
    

    Without strip(), bananas is present in the dictionary but with an empty string as value. With strip(), this code will throw an exception because it strips the tab of the banana line.

提交回复
热议问题