Text translation to a new string in Python. How to transfer text to a new line - instruction

Anonim

In order for in Python to designate the end of one line and start a new one, you need to use a special character. It is important to know how to properly use it in working with various Python files, to the desired moments display it in the console. It is necessary to deal in detail with how to use the separation sign for new rows when working with the program code, is it possible to add text without it.

General information about the symbol of a new line

\ n - designation of information transfer to a new string and closing the old line in Python. This symbol consists of two elements:

  • reverse oblique;
  • N is a symbol from the lower register.

To use this character, you can apply the expression "PRINT (F" HELLO \ NWORLD! ")", At the expense of which you can transfer information to F-lines.

Text translation to a new string in Python. How to transfer text to a new line - instruction 3487_1
Example of using a symbol \ n to distribute the array of information on new line

What is the Print function

Without additional settings, the data transfer symbol to the next string is added in a hidden mode. Due to this it is impossible to see between the rows without activating a specific function. Example Displays the dividing icon in the program code:

Print ("Hello, World"! ") -" Hello, WORLD! "\ N

At the same time, such finding this character is written in the basic characteristics of Python. The "Print" function has a standard value for the "END" parameter - \ n. It is thanks to this function that this character is set at the end of the rows to transfer data to the following lines. Decryption 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 the "\ n" symbol. According to the automatic software code algorithm, it complements the lines at the end, in front of which the "Print" function is prescribed. When using one function "Print", you can not notice the essence of its operation, since only one line will be displayed on the screen. However, if you add some such instructions, the result of the function will be more pronounced:

Print ("Hello, World 1!") Print ("Hello, World 2!") Print ("Hello, World 3!") Print ("Hello, WORLD 4!")

An example of the result prescribed above program code:

Hello, WORLD 1! Hello, WORLD 2! Hello, WORLD 3! Hello, WORLD 4!

Replacing the symbol of a new string through Print

Using the "Print" function, you can not apply the dividing icon between the rows. To do this, in the Function itself it is necessary to change the "END" parameter. In this case, instead of the "END" value, you need to add a space. Due to this, the "END" symbol will be replaced. Result when set to default settings:

>>> Print ("Hello") >>> Print ("WORLD") Hello WORLD

Displaying the result after replacing the "\ N" symbol on a space:

>>> Print ("Hello", End = "") >>> Print ("WORLD") Hello WORLD

An example of using this method for replacing characters to display the sequence of values ​​through one line:

For i in Range (15): if i

Using a dividing symbol 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 considering the document itself, it is impossible to see it through the program code, since such characters are hidden by default. In order to use the new row start symbol, you must create a file filled with names. After its discovery, you can see that all names will begin with 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])

So the names will be displayed only if the text file is set to separate lines in the text file. At the same time, at the end of each previous line, the hidden character "\ n" will be automatically installed. 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. An example of activation of the function:

With Open ("Names.txt", "R") AS F: PRINT (F.Readlines ())

Text translation to a new string in Python. How to transfer text to a new line - instruction 3487_2
Purpose of various characters to work in Python

Division of string for substring

To divide one long line into several settings, you can use the SPLIT method. If you do not make additional edits, the standard separator is a space. After this method is executed, the selected text is divided into separate words on the substitches, converted to the Strings list. As an example:

String = "Some New Text" strings = string.split () Print (Strings) ['Some', 'New', 'Text']

In order to reverse the conversion, with which the list of confirmists will turn into one long string, you must use the JOIN method. Another useful method for working with rows - Strip. With it, you can delete gaps that are located on both sides of the string.

Conclusion

In order to output certain data from a new line when working in Python, it is necessary to finish the old line with the "\ N" symbol. With it, the information standing after the sign is transferred to the next line, and the old closes. However, it is not necessary to use this symbol to transfer data. To do this, you can use the END = "" parameter. The value "Character" and is a dividing symbol.

Message Translation of text to a new string in Python. How to transfer text to a new line - the instruction appeared first to information technology.

Read more