๐ Python Convert String to Array โ Words, Characters, Numbers
๐งฒ Introduction โ Why Convert Strings to Arrays?
Converting a string to an array (or list) is useful in many real-world scenarios:
- ๐งช Text and data processing
- ๐ค Splitting input into characters or words
- ๐งฎ Converting numeric strings to arrays for computation
- ๐ Feeding structured data into ML or algorithms
Python offers flexible and clean ways to turn strings into lists, arrays, or NumPy structures, depending on your use case.
๐ฏ In this guide, youโll learn:
- Convert string to character list
- Convert string to list of words
- Use
array
module for typed arrays - Use NumPy for numeric arrays
- Handle whitespace, delimiters, and numbers
โ 1. Convert String to Character Array (List of Letters)
text = "Python"
char_list = list(text)
print(char_list) # ['P', 'y', 't', 'h', 'o', 'n']
โ
list(string)
breaks the string into individual characters.
๐ก 2. Convert String to List of Words
sentence = "Python is fun"
word_list = sentence.split()
print(word_list) # ['Python', 'is', 'fun']
โ
split()
separates based on spaces by default.
๐งพ Split with a Custom Delimiter
data = "apple,banana,grape"
fruits = data.split(",")
print(fruits) # ['apple', 'banana', 'grape']
๐งฎ 3. Convert Numeric String to Array of Numbers
num_string = "1 2 3 4 5"
nums = [int(n) for n in num_string.split()]
print(nums) # [1, 2, 3, 4, 5]
โ Converts each string item into an integer using list comprehension.
๐ฆ 4. Use array
Module for Typed Arrays
from array import array
num_string = "1 2 3"
nums = array('i', [int(n) for n in num_string.split()])
print(nums) # array('i', [1, 2, 3])
โ Useful for memory-efficient integer or float arrays.
๐ 5. Use NumPy for Numeric Array Conversion
import numpy as np
num_string = "1 2 3 4"
nums = np.fromstring(num_string, sep=' ')
print(nums) # [1. 2. 3. 4.]
โ Ideal for scientific or ML applications needing NumPy arrays.
๐ง 6. Convert String to Byte Array
s = "hello"
byte_arr = bytearray(s, "utf-8")
print(byte_arr) # bytearray(b'hello')
โ Useful for encoding text for networking or cryptography.
๐ Best Practices
โ Do This | โ Avoid This |
---|---|
Use split() for words | Manually indexing strings to split |
Use list comprehension for numeric arrays | Mixing string and int without casting |
Use array for typed storage | Using generic lists when memory matters |
Use NumPy for performance and math | Avoid NumPy for basic string manipulation |
๐ Summary โ Recap & Next Steps
Python offers versatile tools to convert strings to arrays whether you want characters, words, or typed numeric values.
๐ Key Takeaways:
- โ
Use
list()
to get characters - โ
Use
split()
to get words or values - โ Use list comprehension for numbers
- โ
Use
array.array()
or NumPy for numeric efficiency
โ๏ธ Real-World Relevance:
Used in text parsing, data loading, command line inputs, machine learning, and I/O handling.
โ FAQ โ Python Convert String to Array
โ How do I split a string into characters?
โ Use:
list("hello") # ['h', 'e', 'l', 'l', 'o']
โ How do I split a sentence into words?
โ Use:
sentence.split()
โ How do I convert a string of numbers into an integer list?
โ Use:
[int(n) for n in "1 2 3".split()]
โ Whatโs the difference between list()
and split()
?
list("abc")
gives['a', 'b', 'c']
"a b c".split()
gives['a', 'b', 'c']
by whitespace
โ How do I convert a string to a NumPy array?
โ Use:
np.fromstring("1 2 3", sep=" ")
Share Now :