How To Convert Python String To Array

In this tutorial, you will learn to convert Python string to array in different ways. I will be discussing a few methods that you would like the string to be split into as an array.

In Python, there is an inbuilt function known as split[1]. This method is part of the String class, and if used on any string without any input parameter, then it splits the string into a list of strings divided by the space between words present.

Let us see with code examples for various scenarios that you can use to convert Python String to Array.

Using Split() Convert Python String to Array

Since the split() function in Python returns a list of the words contained within the string, using the delimiter string sep as the delimiter string. Let us see the syntax for the split method first and understand it.

Syntax:

str.split(sep=None, maxsplit=- 1)

Where,
   str = The String that you want to Split.
   sep = If you want to use any specific delimiter.
   maxsplit = it specifies how many split of string you want. -1 indicates full             string.

As you understood the syntax and the input parameters to the split function, which is sep and maxsplit, Let me show you how you can split a given string into words using this method.

Code Example: Convert String To List Of Words In Python

#Initializing Given String
givenString = "You are Visiting Coduber To Learn Python."

#Now Split the above string into List of Words
#Using Split Method
arrayOfWords = givenString.split()

#Printing the Array of Words
print(arrayOfWords)

Output:

['You', 'are', 'Visiting', 'Coduber', 'To', 'Learn', 'Python.']

As you can see in the above code, I was able to get the array of words by using the split method on the given string. Now, as in the above code, I haven’t used any of the input parameters, so I was able to split the full string. 

Now, what if you want to split the words only up to 4 words? Then you will need to use the maxsplit input parameter inside the split method. Let us see in the below example code the usage of the maxsplit input parameter in Python.

#Initializing Given String
givenString = "You are Visiting Coduber To Learn Python."

#Now Split the above string into List of Words
#Using Split Method
arrayOfWords = givenString.split(maxsplit=3)

#Printing the Array of Words
print(arrayOfWords)

Output:

['You', 'are', 'Visiting', 'Coduber To Learn Python.']

So, I was able to split the above string into four parts only using the maxsplit input parameter. Similarly, if you want to limit the split of your string, you can use the maxsplit parameter as used above.

If the given string is separated by commas or any other special character other than spaces, you must use the sep parameter in the split function’s input parameter. And in that, you will be required to specify the character that will be treated as a point of the split, ignoring that character.

Let us see in the below example code the usage of sep inside the split method in Python.

#Initializing Given String
givenString = "You,are,Visiting,Coduber,To,Learn,Python."

#Now Split the above string into List of Words
#Using Split Method
arrayOfWords = givenString.split(sep=',')

#Printing the Array of Words
print(arrayOfWords)

Output:

['You', 'are', 'Visiting', 'Coduber', 'To', 'Learn', 'Python.']

So, even if our given string was separated by commas, I was able to split those into an array of words using the sep inside the split function of Python.

How To Convert Python String To Array

Code Example: Convert Python String To List Of Chars With Split Method

Let us see in the code example below how you can use the split method to convert the given string into a list or array of characters in Python. Note that you cannot achieve this with the traditional inbuilt split function, and hence you will need to rewrite the method to get the list of characters from the given string.

def split(inputWord):
    return [char for char in inputWord]
    
#Initializing Given String
givenString = "Coduber"

#Typecasting the Above String
#Into a List.

arrayOfChars = split(givenString)

#Printing the Array of Chars
print(arrayOfChars)

Output:

['C', 'o', 'd', 'u', 'b', 'e', 'r']

How To Convert Python String To List Without Using Split

You can convert the Python String To list without using the split method as well. I will show you examples of how you can achieve it.

Let us see the various code examples for this problem statement.

Code Example: Convert Python String To Array Of Chars (Without Split)

Now, if you want to convert the given string in Python to an array of characters or a list of characters, then in this case you will need to use the list operator on the string.

All you are required to do is just typecast the given string into a list in Python. And this list will then convert all the characters present in the string into an array of characters stored in that list.

Let us see in the below code example how to convert Python String To Array of Chars.

#Initializing Given String
givenString = "Coduber"

#Typecasting the Above String
#Into a List.

arrayOfChars = list(givenString)

#Printing the Array of Chars
print(arrayOfChars)

Output:

['C', 'o', 'd', 'u', 'b', 'e', 'r']

As you can see in the above code, without using the split function I was able to convert the Python String into an array of chars or characters.

Code Example: Convert Python String To List Without Split

Now if you want to convert a given string into a list of words without using the split method in Python then alternatively you can write your loop to check the spaces and then split the word accordingly.

Let us see in the below example code how to use the for loop to manually split the string into a list based upon the spaces.

#Initializing Given String
givenString = "You are Visiting Coduber To Learn Python"

#Typecasting the Above String
#Into a List.
preWord = ''
arrayOfWords = list()
for chars in givenString:
    if chars==' ' and len(preWord)!=0:
        arrayOfWords.append(preWord)
        preWord = ''
    elif chars != ' ':
        preWord += chars

if preWord != ' ' and len(preWord)!=0:
    arrayOfWords.append(preWord)
    preWord = ''

#Printing the Array of Chars
print(arrayOfWords)

Output:

['You', 'are', 'Visiting', 'Coduber', 'To', 'Learn', 'Python']

As you can see in the above code without using the split function the code becomes a lot longer and also the above code does not confirm whether it takes care of all the corner cases or not.

Wrap Up

I hope you got your answer related to how to convert Python String into Array. As I have listed down four different methods and code examples for you to refer to.

If you know any better method than the one discussed above then please let me know in the comment section I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Code Example: How To Check If Set Is Empty In Python
  2. Code Examples – Write A List To CSV Using Python
  3. Code Example: Loop Back To The Beginning Of A Program
  4. How To Do Nothing In An If Statement Python
  5. Code Example: Remove The First Item From List Python
  6. 4 Ways To Loop Through List With Index Using Python
  7. Code Examples: Multiply In Python
  8. How To Join Words In a List Using Python Code Example
  9. Python Code Example: How To Find Element in a List in 6 Way
  10. How To Iterate Over Rows in a DataFrame in Pandas Python
How To Convert Python String To Array

Leave a Comment