VOOZH about

URL: https://www.geeksforgeeks.org/python/difference-between-strip-and-split-in-python/

⇱ Difference Between strip and split in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference Between strip and split in Python

Last Updated : 13 Nov, 2024

The major difference between strip and split method is that strip method removes specified characters from both ends of a string. By default it removes whitespace and returns a single modified string. Whereas, split method divides a string into parts based on a specified delimiter and by default it splits by whitespace and returns a list of strings.

Example of strip()


Output
hello world
hello

Explanation:

  • s.strip() removes spaces from both beginning and end of the string and results in "hello world".
  • strip("*") removes * characters from both ends and results in "hello".

Example of split()


Output
['hello', 'world']
['apple', 'orange', 'banana']

Explanation:

  • s.split() basically splits "hello world" into two elements based on whitespace and results in ['hello', 'world']
  • split(',') divides string at each comma and results in list ['apple', 'orange', 'banana']
Comment