Home »
Python »
Python Programs
Python NumPy - Vertical Stacking of String Characters
By IncludeHelp Last updated : September 14, 2024
Problem Statement
Given a list of strings, and write Python code to split this string into individual characters and each character is stacked vertically.
Vertical Stacking of String Characters
To create vertical stacking of string characters, we can define a function where we can use a list comprehension to split each input string into a list of individual characters. It then passes this list of lists to numpy.vstack() to stack the characters vertically into a 2-D NumPy array.
Python Code to Vertical Stacking of String Characters
Let us understand with the help of an example:
import numpy as np
# Input string
string = "Hello, World!"
# Split the string into individual characters
char_array = np.array(list(string))
# Vertically stack the characters
vertical_stack = np.vstack(char_array)
# Print the result
print(vertical_stack)
Output
[['H']
['e']
['l']
['l']
['o']
[',']
[' ']
['W']
['o']
['r']
['l']
['d']
['!']]
Python NumPy Programs »