👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I have some python code that splits on comma, but doesn"t strip the whitespace:
>>> string = "blah, lots , of , spaces, here "
>>> mylist = string.split(",")
>>> print mylist
["blah", " lots ", " of ", " spaces", " here "]
I would rather end up with whitespace removed like this:
["blah", "lots", "of", "spaces", "here"]
I am aware that I could loop through the list and strip() each item but, as this is Python, I"m guessing there"s a quicker, easier and more elegant way of doing it.
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Split by comma and strip whitespace in Python, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Split by comma and strip whitespace in Python
- Deutsch Split by comma and strip whitespace in Python
- Français Split by comma and strip whitespace in Python
- Español Split by comma and strip whitespace in Python
- Türk Split by comma and strip whitespace in Python
- Русский Split by comma and strip whitespace in Python
- Português Split by comma and strip whitespace in Python
- Polski Split by comma and strip whitespace in Python
- Nederlandse Split by comma and strip whitespace in Python
- 中文 Split by comma and strip whitespace in Python
- 한국어 Split by comma and strip whitespace in Python
- 日本語 Split by comma and strip whitespace in Python
- हिन्दी Split by comma and strip whitespace in Python
Tallinn | 2023-01-31
Simply put and clear. Thank you for sharing. Split by comma and strip whitespace in Python and other issues with code Python module was always my weak point 😁. Will use it in my bachelor thesis
Warsaw | 2023-01-31
Simply put and clear. Thank you for sharing. Split by comma and strip whitespace in Python and other issues with StackOverflow was always my weak point 😁. Will use it in my bachelor thesis
London | 2023-01-31
I was preparing for my coding interview, thanks for clarifying this - Split by comma and strip whitespace in Python in Python is not the simplest one. Checked yesterday, it works!