Introduction to Python string to array
In this article, we will discuss a string to be converted to an array in Python. In general, we know that an array is a data structure that has the capability of storing elements of the same data type in Python, whereas the list contains elements with different data type values. In this, we have to see how to convert a string to an array. We have to note that how we can split the given string into an array; it can be a set of characters or strings. This conversion can be done differently; the main technique is to use the split function to convert the string to an array.
How to convert a string to an array in python?
In this article, we are discussing on a string to an array. To do this, we are using the split() function for converting a string to an array. Now let us see below how to convert a single string to an array of characters, but we will use a simple function instead of the split() function in the below example.
Example #1
Code:
def split_str(s):
return [c for c in s]
s = 'Educba Training'
print("The given string is as follows:")
print(s)
print("The string converted to characters are as follows:")
print(split_str(s))
Output:
In the above program, we are splitting the given string into characters without using the split() function. In the above program, we can see we have created a function named “split_str” where we are passing a string, and it returns an array of characters. In the above screenshot, we can see the given string results into single characters.
Example #2
Now let us see how to use the split() function to split the string to array in the below example that is demonstrated as below:
Syntax:
split(separator, maxsplit)
Code:
t = "Educba Training"
print("The given strings is as follows:")
print(t)
x = t.split()
print("The array of strings after using split function:")
print(x)
Output:
In the above example, we can see we have the given string as “Educba Training,” which means there are two strings, and it is considered as a single string which is stored in the variable “t.” Then we have applied the split() function on the variable “t,” and the result is stored in another variable, “x.” Hence the output will be displayed as an array of strings such as “ [‘Educba,’ ‘Training’].”
Suppose if we have CSV strings, then also we can apply a split() function to these strings and obtain the array of strings, but we have to specify the separator of each string as “,.”
Example #3
Let us see an example below with CSV formatted string and converted to an array of strings using the same split() function.
Code:
str1 = "Educba, Training, with, article, on, Python"
print("The given csv string is as follows:")
print(str1)
str2 = str1.split(",")
print("The csv string is converted to array of string using split is as follows:")
print(str2)
Output:
In the above program, we can see str1 holds the CSV formatted string, which means comma-separated string, so to obtain the array of the string; first, we have to separate it from comma at each word or string in the given string. So when the split() function is applied on such string and we have specified (“,”) comma as delimiter or separator to obtain the array of string.
By default, when we specify or apply the split() function on any string, it will by default take “white space” as separator or delimiter. Hence if we have any string having any special characters and we want only to extract an array of strings, then we can just specify that special character as delimiter or separator to obtain the array of strings. We will see a simple example with some special characters in the given string. We need to obtain only the array of strings; then, we can do it again by applying the split() function with delimiter or separator special character in the given string.
Example #4
Code:
str1 = "Educba #Training #with #article #on #Python"
print("The given string with special character is as follows:")
print(str1)
str2 = str1.split("#")
print("The given string is converted to array of string using split() is as follows:")
print(str2)
Output:
In the above program, we can see we have a given string with each string having special characters such as a hash (“#”) separated string. This string which is stored in the variable “str1” and split function applied on this string with separator or delimiter specified as (“ # ” ), and the result obtained is stored in another string str2. This string “str2” contains the array of strings separated by the special characters in the given string. Hence the result is as shown in the above screenshot, which has an array of strings from the given string having special characters.
Conclusion
In this article, we have seen what an array is and how to convert any string into an array. Firstly we have seen how to convert a given single string into characters by using the “for” loop. Then we have seen how to use the split() function to convert any string into an array of strings. First, we have seen how to use the split function on the string without specifying any separator or delimiter, then we have seen how to apply a split function on the CSV formatted string to obtain an array of string, and last we have also seen that this split() function can be used on any string having any kind of special characters in it to obtain the only array of strings.
Recommended Articles
This is a guide to Python string to an array. Here we discuss the introduction and examples to convert a string to an array along with code implementation. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses