❮ 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
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
Given a string, write a Python program to split the characters of the given string into a list using Python.
Examples:
Input : geeks Output : ['g', 'e', 'e', 'k', 's'] Input : Word Output : ['W', 'o', 'r', 'd']
Method 1: Split a string into a Python list using unpack[*] method
The act of unpacking involves taking things out, specifically iterables like dictionaries, lists, and tuples.
Python3
string
=
"geeks"
print
[[
*
string]]
Output:
['g', 'e', 'e', 'k', 's']
Method 2: Split a string into a Python list using a loop
Here, we are splitting the letters using the native way using the loop and then we are appending it to a new list.
Python3
string
=
'geeksforgeeks'
lst
=
[]
for
letter
in
string:
lst.append[letter]
print
[lst]
Output:
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
Method 3: Split a string into a Python list using List Comprehension
This approach uses list comprehension to convert each character into a list. Using the following syntax you can split the characters of a string into a list.
Python3
string
=
"Geeksforgeeks"
letter
=
[x
for
x
in
string]
print
[letter]
Output:
['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
Method 4: Split a string into a Python list using a list[] typecasting
Python provides direct typecasting of strings into a list using Python list[].
Python3
def
split[word]:
return
list
[word]
word
=
'geeks'
print
[split[word]]
Output:
['g', 'e', 'e', 'k', 's']
Method 5: Split a string into a Python list using extend[]
Extend iterates over its input, expanding the list, and adding each member.
Python3
string
=
'Geeks@for'
lst
=
[]
lst.extend[string]
print
[lst]
Output:
['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']
Sometimes we need to work with just the lists and hence strings might need to be converted into lists. It has to be converted in list of characters for certain tasks to be performed. This is generally required in Machine Learning to preprocess data and text classifications. Let’s discuss certain ways in which this task is performed.
Method #1: Using list slicing
List slicing can be used for this particular purpose, in which we assign to each index element of list the next occurring character of string using the slice operation.
Python3
test_string
=
"GeeksforGeeks"
print
[
"The original string is : "
+
str
[test_string]]
res
=
[]
res[:]
=
test_string
print
[
"The resultant list of characters : "
+
str
[res]]
Output
The original string is : GeeksforGeeks The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Method #2: Using list[]
The most concise and readable way to perform splitting is to type case string into list and the splitting of list is automatically handled internally. This is recommended method to perform this particular task.
Python3
test_string
=
"GeeksforGeeks"
print
[
"The original string is : "
+
str
[test_string]]
res
=
list
[test_string]
print
[
"The resultant list of characters : "
+
str
[res]]
Output
The original string is : GeeksforGeeks The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Method #3: Using map[] + lambda
This is yet another way to perform this particular task. Though not recommended but can be used in certain situations. But drawback is readability of code gets sacrificed.
Python3
test_string
=
"GeeksforGeeks"
print
[
"The original string is : "
+
str
[test_string]]
res
=
list
[
map
[
lambda
i:i, test_string]]
print
[
"The resultant list of characters : "
+
str
[res]]
Output
The original string is : GeeksforGeeks The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Method#4: Using join[] + split[] method
We can use above mention method to split the string in list of character. Join is used to add the space between the character of string and split is used to split the character between space in string.
Python3
test_string
=
"GeeksforGeeks"
print
[
"The original string is : "
+
str
[test_string]]
test_string
=
" "
.join[test_string]
res
=
test_string.split[
" "
]
print
[
"The resultant list of characters : "
+
str
[res]]
Output
The original string is : GeeksforGeeks The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Method#5: Using re.findall[] method: We can use above mention method to find all character in the string and form a list of characters.
Python
import
re
test_string
=
"GeeksforGeeks"
print
[
"The original string is : "
+
str
[test_string]]
res
=
re.findall[r
'[a-zA-z]'
, test_string]
print
[
"The resultant list of characters : "
+
str
[res]]
Output
The original string is : GeeksforGeeks The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']