Signup/Sign In

How To Convert String to String Array in Python

The most commonly used data type in any project and in any programming language is string only. In this tutorial, we will learn how to convert a string to a string array using the python built-in functions such as split(), rsplit(), partition(), rpartition() methods.

Note: In Python, Array is represented by List. So, we will work on the list throughout the article.

The split() method splits the specified string according to the separator and the default separator is space. The split() method returns the list data type. The rsplit() method is the same as the split() method, but it works from the right side. The partition() method separates string into three parts by using the given separator, the first part consists of the part before the specified separator, the second part consists of the specified separator, the third part consists of the part after the specified separator and it returns tuple as output. The rpartition() method is the same as the partition() method, but it works from the right side.

Example: Converting a string to string array using the split() method

The below example shows how to convert a string to a string array using the split() method.

#Defining string
string="study tonight"
print("The string is :",string)
print("The class type is:",type(string))
#converting string using split() method
result=string.split()
print("string converted to list:",result)
print("The class type is",type(result))

In the above example, first, we defined the string in the variable string and checking its class type. The class type is the string before the conversion. Next, using the split() method, we converted the string to a list. The split() method considers space as a separator and split the string accordingly. The converted string is stored in the variable result. The class type is list after the conversion.


The string is: study tonight
The class type is: <class 'str'>
string converted to list: ['study', 'tonight']
The class type is <class 'list'>

Example: Converting a string to a string array

The below example shows how to convert a string to a string array using the split() method with the specified operator.

#Defining string
string="www.studytonight.com"
print("The string is :",string)
print("The class type is",type(string))
#converting string using split() method
result=string.split(".")
print("string converted to list:",result)
print("The class type is",type(result))

In the above example, we consider www.studytonight.com as a string to convert it to a list. As we are given “.” as a separator, the split() method splits the string according to the separator.


The string is: www.studytonight.com
The class type is <class 'str'>
string converted to list: ['www', 'studytonight', 'com']
The class type is <class 'list'>

Example: Converting a string to string array using the rsplit() method.

The below example shows how to convert string to list using the rsplit() method.

#Defining string
string="www.studytonight.com"
print("The string is :",string)
print("The class type is",type(string))
#converting string using split() method
result=string.rsplit(".",1)
print("string converted to list:",result)
print("The class type is",type(result))

In the above example, we used the rsplit() method, which splits the defined string from the right side. As we gave maxsplit value 1, it splits the string from the right side only once, according to the separator.


The string is: www.studytonight.com
The class type is <class 'str'>
string converted to list: ['www.studytonight', 'com']
The class type is <class 'list'>

Example: Converting a string to tuple using the partition() method.

In case, you want to get result as a tuple rather than list then use the partition() method.

string_1="www.studytonight.com"
print("The string is :",string_1)
print(type(string_1))
list_1=string_1.partition(".")
print("string converted to list:",list_1)
print(type(list_1))

We consider studytonight.com as a string that is stored in a variable string. We applied the partition() method with a separator “.” on the string and it returns a tuple that consists of three parts.


The string is : www.studytonight.com
<class 'str'>
string converted to list: ('www', '.', 'studytonight.com')
<class 'tuple'>

Conclusion:

In this tutorial we learned how to convert the string to string array using the python built-in functions. We solved many examples on the built-in functions such as list(), split(), rsplit(), partition(), rpartition() methods.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.