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
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.