❮ String Methods
Example
Split a string into a list where each word is a list item:
txt = "welcome to the jungle"
x = txt.split()
print(x)
Try it Yourself »
Definition and Usage
The split()
method splits a string into a list.
You can specify the separator, default separator is any whitespace.
Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Syntax
string.split(separator, maxsplit)
Parameter Values
Parameter | Description |
separator | Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator |
maxsplit | Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences" |
More Examples
Example
Split the string, using comma, followed by a space, as a separator:
txt = "hello, my name is Peter, I am 26 years old"
x = txt.split(", ")
print(x)
Try it Yourself »
Example
Use a hash character as a separator:
txt = "apple#banana#cherry#orange"
x = txt.split("#")
print(x)
Try it Yourself »
Example
Split the string into a list with max 2 items:
txt = "apple#banana#cherry#orange"
# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split("#", 1)
print(x)
Try it Yourself »
❮ String Methods
W3schools Pathfinder
Track your progress - it's free!
FAQs
Python String split() Method Syntax
maxsplit: It is a number, that tells us to split the string into a maximum of the provided number of times. If it is not provided then the default is -1 which means there is no limit.
How to fix ValueError not enough values to unpack expected 2 got 1? ›
Here, you are trying to unpack the results of the split() method into two variables (schema and table), but it looks like the split() method is only returning one value instead of two. To fix this error, you can check the value of table_name before calling the split() method to make sure it contains a dot (.)
What is not enough values to unpack in Python? ›
Your error 'ValueError: not enough values to unpack' generally occurs when you're trying to unpack more values than those available in the iterable. In the context of YOLOv8, this likely means that there is an issue with the format of the labels in your dataset.
How do you split a string with multiple conditions in Python? ›
Splitting the String Based on Multiple Delimiters:
Multiple delimiters can be specified as a parameter to the split() function by separating each delimiter with a |. The given string or line with multiple delimiters is separated using a split function called re. split() function.
Can you split by multiple things in Python? ›
Python Split By Multiple Characters using Re.
In Python, we can split multiple characters from a string using resplit(). This is the most efficient and commonly used method to split multiple characters at once. It makes use of regex(regular expressions) in order to do this.
How does split() work in Python? ›
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
How do you solve too many values to unpack in Python? ›
Solution. Option 1: Assign return values to three variables. Now that Python can link the return of sum , product , and quotient directly to result_1 , result_2 , and result_3 , respectively, the program can run without error. Option 2: Use an underscore to throw away a return value.
How do I fix too many values to unpack in Python? ›
The “valueerror: too many values to unpack (expected 2)” error occurs when you do not unpack all the items in a list. This error is often caused by trying to iterate over the items in a dictionary. To solve this problem, use the items() method to iterate over a dictionary.
What is the string split function? ›
Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.
How to split a single word string in Python? ›
split('x') can be used to split a string on a specific substring 'x'. Without 'x' specified, . split() simply splits on all whitespace, as seen above.
Here are some crucial things for take-away:
- A single asterisk * unpacks items from lists, tuples, sets, and strings;
- A double asterisk ** unpacks dictionaries;
- Using a single asterisk on a dictionary will unpack only dictionary keys;
- You can pass all iterable items to a function with the help of unpacking operators;
What does the unpack () function do Python? ›
Unpacking in Python refers to the process of taking elements from iterable objects like tuples, lists, or dictionaries and assigning them to variables. This feature simplifies the handling of compound data structures and enhances code readability.
What does unpack mean in Python? ›
In Python, unpacking is the process of extracting values from a sequence and assigning them to multiple variables. We achieve unpacking with the assignment operator on the left side and an iterable object on the left side.
What are the three possibilities to consider if a function is not working in Python? ›
If a function is not working, there are three possibilities to consider:
- There is something wrong with the arguments the function is getting; a precondition is violated.
- There is something wrong with the function; a postcondition is violated.
- There is something wrong with the return value or the way it is being used.