Computer Science
Explain function split() with an example.
Python String Manipulation
3 Likes
Answer
The split() function breaks up a string at the specified separator and returns a list of substrings. The syntax is str.split([separator, [maxsplit]]). The split() method takes a maximum of 2 parameters:
separator (optional) — The separator is a delimiter. The string splits at the specified separator. If the separator is not specified, any whitespace (space, newline, etc.) string is a separator.
maxsplit (optional) — The maxsplit defines the maximum number of splits. The default value of maxsplit is -1, which means no limit on the number of splits.
For example,
x = "blue;red;green"
print(x.split(";"))
Output
['blue', 'red', 'green']
Answered By
2 Likes
Related Questions
Assertion (A): The following code snippet on execution shall return the output as: False True
str1 "Mission 999" str2 = "999" print(str1.isdigit(), str2.isdigit())Reasoning (R): isdigit() function checks for the presence of all the digits in an inputted string.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
How does '*' operator behave on strings?
How many times is the word 'HELLO' printed in the following statement?
s = "python rocks" for ch in s: print ("Hello")Write the output of the following:
>>> x = "hello" >>> print(x[1:-2])