DEV Community

pythonpipblog
pythonpipblog

Posted on

How To Convert Python String To Array

This tutorial help to create python string to array.Python does not have inbuilt array data type, it have list data type that can be used as array type.So basically, we are converting string into python list.

Python String to Array
We ll convert String to array in Python. The python string package have split() method. The String.split() method splits the String from the delimiter and returns as a list items. The split() method default separator is the whitespace but you can specify the separator.

Syntax
string.split(separator, maxsplit)

Where is parameters are :

separator(optional) : It used to split the String.
maxsplit(optional) : It specifies number of splits to do. The default value is -1, which is “all occurrences”.
Example:
1

test.py

2
str = "My name is dominic toretto"
3
arr = str.split()
4
print(arr)
Output
['My', 'name', 'is', 'dominic', 'toretto']

Originally posted at - https://www.pythonpip.com/python-tutorials/how-to-convert-python-string-to-array/

Top comments (0)