my_str.split(delimiter,max_value)
delimiter : (default is space), required, to be used to break the stringmax_value : optional , the number of elements in output plus one. Default value is -1 so it includes all occurrence.
my_list="'Alex','Ronald','John'"
my_list=my_list.split(',')
print(my_list)
my_list="'Alex','Ronald','John'"
my_list=my_list.split(',',1)
print(my_list)
output
["'Alex'", "'Ronald'", "'John'"]
["'Alex'", "'Ronald','John'"]
By using rsplit() in place of split(), the output will change like this. ( difference between rsplit() and split() )
["'Alex'", "'Ronald'", "'John'"]
["'Alex','Ronald'", "'John'"]
text = "apple orange banana"
fruits = text.split()
print(fruits)
Output:
['apple', 'orange', 'banana']
data = "name,age,location"
values = data.split(',')
print(values)
Output:
['name', 'age', 'location']
text = "apple-orange-banana-pear"
result = text.split('-', 2)
print(result)
Output:
['apple', 'orange', 'banana-pear']
text = "apple-orange-banana-pear"
result = text.rsplit('-', 2)
print(result)
Output:
['apple-orange', 'banana', 'pear']
phrase = input("Enter a phrase: ")
words = phrase.split()
print("Words:", words)
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.