How To Split String In Python Example

02-Sep-2021

.

Admin

Hello Friends,

Now let's see example of how to split string using python. We will show split string character in python. I am going to learn you python split() method exaple.

The split() method in Python split a string into a list of strings after breaking the given string by the specified separator.


Syntax : str.split(separator, maxsplit)

Parameters :

separator : This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.

maxsplit : It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then the default is -1 that means there is no limit.

Returns : Returns a list of strings after breaking the given string by the specified separator.

Here i will give you two example for split string in python. So let's see the bellow example:

Example 1

text = 'a b c'

# Splits at space

print(text.split())

word = 'd, e, f'

# Splits at ','

print(word.split(','))

Output:

['a', 'b', 'c']

['d', 'e', 'f']

Example 2

word = 'This, is, python, example'

# maxsplit: 0

print(word.split(', ', 0))

# maxsplit: 4

print(word.split(', ', 4))

# maxsplit: 1

print(word.split(', ', 2))

Output:

['This, is, python, example']

['This', 'is', 'python', 'example']

['This', 'is', 'python, example']

It will help you....

#Python