August 17, 2024
Python split function
split() function is one of the most important Python’s built in function of string manipulation. Sometimes it happens that you want to convert a string's element into an array so for the same you can use split() function in Python. split() function requires a string separator argument and it is called on a string object from which you will create an array. The split() function divide the string in small chunk through the separator string and write in an array element. Every time split() function found the separator string it creates an new array element. The example of split() function is mentioned below:
Code
weekDaysString = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday"
weekDaysArray = weekDaysString.split(", ")
print(weekDaysArray)
In above mentioned example we have a string variable with the name of weekDaysString which have all the week days name in comma separated format. In the next line we have use split function on weekDaysString string variable and provide separator string as comma(,) then split function returns an array of all the week days and we store it in weekDaysArray and print it on the screen through print function.