How to Use String split() Method in PythonPython Basics


The split() method breaks up a string at the specified separator and returns a list of strings.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

str = "have a nice day....wow!!!"
print(str.split())
print(str.split('i', 1))
print(str.split('w'))

Output:

['have', 'a', 'nice', 'day....wow!!!']
['have a n', 'ce day....wow!!!']
['have a nice day....', 'o', '!!!']

Syntax

str.split([separator [, maxsplit]])

Parameters

Name Description
separator A delimiter such as comma.
maxsplit Define the maximum number of splits.

Return Value

It returns a list of strings.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us