Show menu >>

How To Print Output Horizontally In Python

How To Print Output Horizontally In Python What's Up site

The first and second methods are the best fit if you need to change a line during a line reversal. Moreover, they are significantly inferior to the 4 and 5 methods in speed. They are read moderately well, so in some cases they are appropriate to use.
2021-11-21, by ,

#Python || #Tutorial || #How-to ||

Table of contents:



An overview of the three main ways to reverse a Python string. Also known as "slice", reverse iteration and the classic flip-in-place algorithm. You will also see performance metrics for the code you are executing.

What's the best way to flip Python strings? Of course, line flips are not used as often in everyday programming, but this is a common question during interviews, like explained here https://python.engineering/printing-words-vertically/

1. Using a loop with a string as a result

Let's create a function reversed1 with an argument variable, where variable is the variable that holds the string that we want to reverse. Since strings are an immutable object, we will create a separate, for now, empty variable res, which will store the result in the future.

We will put a loop in the function that will "walk" through each of the elements of the string. We start from the end of the line using positive indices, respectively, the start parameter of the range function -len (variable) -1. -1 because the length of the string is always 1 more than the index of its last element. We must end at the first character of the line, so the stop parameter of the range () function is -1, since numbers are listed up to the value of this parameter, not inclusive. The step parameter is -1 because we are counting in the opposite order.

2. Using a loop with a list as a result

This method is similar to the previous one, the only difference is in the data type of the res variable - here it is a list.

Instead of concatenation, you can use the append () method, with which we add the item specified as an argument to the method to the end of the list.

The function is currently returning a list of single-character elements. If we are not satisfied with this, then why not convert the list to a string using the join () method? We do this by adding the construction res = ''. Join (res).

3. Recursion

The third method in our review is recursion, which is, as always, difficult to understand. As always, we create a function, but we are in no hurry to put a loop there.

I'll start the explanation from the end. If we have written all characters except the first into the result, then the length of the remaining string is equal to one and, therefore, it must be returned.

4. Using the built-in function

Python 3 has a built-in special function reversed () that takes a list or string as an argument, and returns an iterator of a sequence of values ​​that consists of all the elements of the argument in reverse order.

In simple words, it is not enough to write res = reversed (variable), the data must be converted to the desired type (in our case, to a string). We can do this using the join () method, joining the sequence through an empty string. After the performed actions, we return the result.