Python Built-in Function Split() and Strip()

Python provide built-in function to support the string operation which can be very useful to know if you're getting you hand on any programming challenge. Today we will discuss about strip and Split methods.

Strip()

syntax- string.strip('char')

Here char would be the character to be removed from the given string. It would return a copy of the given string, char removed from the beginning and end of the string.

if no argument is given, then it will strip the white-space from the beginning or end of the string.

Example:

input_string="pppppLevel-up your python with http://LearningWithPython.comppppp"

input_string.strip('p')

Output:

'Level-up your python with http://LearningWithPython.com'

Split()

syntax- string.split('str')

Here str would be the string by which the main string to be separated. It will return the list with the all word separated by the given str.

Example

input_str='Level up,your,python,with,http://LearningWithPython.com'

Let's separate it based on ','

input_str.split(',')

Output:

['Level up', 'your', 'python', 'with', 'http://LearningWithPython.com']

Take another example:

input_str1="Level up your python with http://LearningWithPython.com"

Now separate it based on 'space'.

input_str1.split(' ')

Output:

['Level', 'up', 'your', 'python', 'with', 'http://LearningWithPython.com']

Usage:- To break down the large string into smaller one or opposite on string concatenation.