Want to split a string?

To split a string, we use the method .split().

This method will return one or more new strings. All substrings are returned in the list datatype.

Note: Split with the method str.split(sep, maxsplit). The 2nd parameter is optional. If maxsplit is set, it returns no more than maxsplit elements.

Related course:

String split

We can split a string based on a character.

s = "To convert the result to"
parts = s.split(" ")

python split string

Any character can be used. If you want to get sentences you could use:

s = "Python string example. We split it using the dot character."
parts = s.split(".")
This will result in:

[‘Python string example’, ‘ We split it using the dot character’, ‘’]