×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

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 »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.