Split Vs Strip In Python To Remove Redundant White Space
May I ask do I need to use strip() before split() to remove any redundant space in Python (and turn into a list after)? For example:
string1 = ' a b '
I want the result:
#list1=[a,b]
When I test I found out that list1=string1.split()
is enough. But somehow my teacher say string1.strip().split()
is needed. Maybe he is wrong?
Answer
According to the documentation:
If sep is not specified or is
None
, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
Which means, that the logic of strip()
is already included into split()
, so I think, your teacher is wrong. (Notice, that this will change in case if you're using a non-default separator.)
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module