Translation of text to a new line in Python. How to move text to a new line – instructions

In Python, to mark the end of one line and start a new one, you need to use a special character. At the same time, it is important to know how to use it correctly when working with various Python files, and display it in the console at the required moments. It is necessary to understand in detail how to use the delimiter for new lines when working with program code, whether it is possible to add text without using it.

General information about the newline character

n is the symbol for wrapping information on a new line and closing the old line in Python. This symbol consists of two elements:

  • reverse oblique;
  • n is a lowercase character.

To use this character, you can use the expression “print(f” HellonWorld!”) ”, Due to which you can transfer information in f-lines.

Translation of text to a new line in Python. How to move text to a new line - instructions
An example of using the character n to distribute an array of information over new lines

What is the print function

Without additional settings, the data transfer character to the next line is added in hidden mode. Due to this, it cannot be seen between the lines without activating a certain function. An example of displaying a separator icon in the program code:

Print (“Hello, World”!”) – “Hello, World!”n

At the same time, such a finding of this character is written in the basic characteristics of Python. The “print” function has a default value for the “end” parameter – n. It is thanks to this function that this character is set at the end of lines to transfer data to the next lines. Explanation of the “print” function:

print(*objects, sep=' ', end='n', file=sys.stdout, flush=False)

The value of the “end” parameter from the “print” function is equal to the character “n”. According to the automatic algorithm of the program code, it completes the lines at the end, before which the “print” function is written. When using a single “print” function, you may not notice the essence of its work, since only one line will be displayed on the screen. However, if you add a few statements like this, the result of the function becomes more explicit:

print("Hello, World 1!")    print("Hello, World 2!")    print("Hello, World 3!")    print("Hello, World 4!")

An example of the result of the code above:

Hello, World 1!    Hello, World 2!    Hello, World 3!    Hello, World 4!

Replacing a newline character with print

Using the “print” function, it is possible not to use a separator character between lines. To do this, you need to change the “end” parameter in the function itself. In this case, instead of the “end” value, you need to add a space. Due to this, it is the space that will replace the “end” character. Result with default settings set:

>>> print("Hello")    >>> print("World")    Hello    World

Displaying the result after replacing the character “n” with a space:

>>> print("Hello", end=" ")    >>> print("World")    Hello World

An example of using this method of replacing characters to display a sequence of values ​​in one line:

for i in range(15):        if i < 14:            print(i, end=", ")        else:            print(i)

Using separator character in files

The symbol after which the text of the program code is transferred to the next line can be found in the finished files. However, without viewing the document itself through the program code, it is impossible to see it, since such characters are hidden by default. In order to use the newline character, you need to create a file filled with names. After opening it, you can see that all names will start on a new line. Example:

names = ['Petr', 'Dima', 'Artem', 'Ivan']    with open("names.txt", "w") as f:        for name in names[:-1]:            f.write(f"{name}n")        f.write(names[-1])

Names will be displayed this way only if the text file is set to separate information into separate lines. This will automatically set the hidden character “n” at the end of each previous line. To see the hidden sign, you need to activate the function – “.readlines()”. After that, all hidden characters will be displayed on the screen in the program code. Function activation example:

with open("names.txt", "r") as f:        print(f.readlines())
Translation of text to a new line in Python. How to move text to a new line - instructions
Assigning Different Symbols to Work in Python

Advice! Actively working with Python, users often encounter situations where the program code must be written in one long line, but it is extremely difficult to review it and identify inaccuracies without separation. So that after dividing a long line into separate fragments, the computer considers it whole, in each free gap between the values, you must insert the character “” - a backslash. After adding a character, you can move to another line, continue writing code. During launch, the program itself will assemble the individual fragments into a single line.

Splitting a string into substrings

To split one long string into several substrings, you can use the split method. If no further edits are made, the default delimiter is a space. After executing this method, the selected text is divided into separate words by substrings, converted into a list of strings. As an example:

string = "some new text"    strings = string.split()    print(strings)    ['some', 'new', 'text']

In order to carry out the reverse transformation, with the help of which the list of substrings will turn into one long string, you must use the join method. Another useful method for working with strings is strip. With it, you can remove spaces that are located on both sides of the line.

Conclusion

In order to output certain data from a new line when working in Python, it is necessary to end the old line with the character “n”. With its help, the information after the sign is transferred to the next line, and the old one is closed. However, it is not necessary to use this symbol to transfer data. To do this, you can use the parameter end = "". The value "character" is the separator character.

Leave a Reply