Interconvert Horizontal and Vertical String using Python


In Python, we have some built−in functions like str(), replace(), split(), strip(), and, len() that will be used to solve Interconvert Horizontal and vertical String. Python allows us to convert strings between horizontal and vertical instructions with simple steps by improving readability, formatting, text manipulation, visual creativity, and programming applications.

Syntax

The following syntax is used in the examples-

str()

This is an in−built function in Python that converts the value to a string. It prints the value as a string.

replace("\n", "")

The replace is a built−in method in Python that accepts two parameters− \n and "" that means the escape sequence will be replaced by an empty string.

split()

The split() is a built−in method in Python that can be used to separate all the words from the string and return a new list of substrings.

strip()

The built−in method strip is defined by removing spaces from the string.

len()

The len() is a built−in method in Python that can be used to return the length of a string.

Using for and While Loop

The program uses the for loop to iterate into individual characters from the string and it helps to convert the horizontal string into vertical. Secondly, in another example, we will show the program based on a while loop.

Example

In the following example, we will start the program by storing the input string in the variable my_string and display the original input. Then print the statement for the vertical string and using for loop it will iterate each character of the input string and make them vertical. Finally, print the result with the help of variable x.

my_string = "Tutorialspoint"
print("Horizontal String:", my_string)
print("The Vertical String becomes-")
for x in my_string:
  print(x) 

Output

Horizontal String: Tutorialspoint
The Vertical String becomes-
T
u
t
o
r
i
a
l
s
p
o
i
n
t

Example

In the following example, we will start initializing the variable my_string to store the input value. Then initialize the index with the initial value i.e. 0 and it will keep track of iteration. Now using the while loop it sets the condition by using the < operator. Next, use the index as a parameter of variable my_string and increment the index by 1 to get the final result in the form of a vertical string.

my_string = "Tutorialspoint"
index = 0
while index < len(my_string):
    print(my_string[index])
    index += 1

Output

T
u
t
o
r
i
a
l
s
p
o
i
n
t

Using replace() and New Line

The program uses replace to remove the new line(\n) and store the new line by empty string which will help to convert horizontal into vertical string.

Example

In the following example, start the program by initializing the input string with the help of an escape sequence i.e. \n(new line), and display the original string. Next, the input value is converted to a string with the help of the built−in function str(). Then use the function named replace() which will replace the \n(new line) by “”(empty string) and store it in a specific variable. Then use the same variable in the print function to get the desired result.

# Initializing the vertical string by using \n
my_str = "\nT\nU\nT\nO\nR\nI\nA\nL\nS\nP\nO\nI\nN\nT"
# printing original String
print("The original string become:" + str(my_str))
# using replace() + "\n" to solve this problem
res = my_str.replace("\n", "")
# Final result
print("The vertical string converted to horizontal string:\t" + str(res))

Output

The original string become:
T
U
T
O
R
I
A
L
S
P
O
I
N
T
The vertical string converted to horizontal string:	 TUTORIALSPOINT

Using split() and strip() Function

The program uses built−in function split() to separate each character whereas strip() method removes the whitespace and this will help to interconvert Horizontal and Vertical String.

Example

In the following example, we will start the program by defining a multi−line string in the variable v_str. Then store the empty string in the variable h_str which will store the string as horizontal. Next, using for loop variable ln iterate over the split() method called on v_str with the argument "\n", which splits the string into a list of lines. Then use the method strip() while incrementing the resulting line added to the variable h_str. The strip() method removes the whitespaces. Finally, we are printing the result with the help of variable h_str.

v_str = """
A
P
P
L
E
"""
h_str = ""
for ln in v_str.split("\n"):
    h_str += ln.strip()
print(h_str)

Output

APPLE

Conclusion

We discussed the various ways to solve the problem statement. Beginning with loops it iterates over each character from the line string whereas using the built−in function has another way to sort the problem. So, changing the text depending on specific needs and preferences.

Updated on: 14-Aug-2023

641 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements