This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Friday, January 10, 2020

create a multi-line string in python

create a multi-line string in python




Creating a Multiline String If you create a string using single or double quotes, the whole string must onto a single line.

Here's what happens when you try to stretch a string across multiple lines:
>>> 'one File "<stdin", line 1
'one
SyntaxError: EOL while scanning string literal



As we saw in Section 4.1. Creating Strings of characters, on page 65. LOL stands for "end of line": so in this error report, Python is saying that it reached the end of the line before it found the end of the string.
To span multiple lines, put three single quotes or three double quotes around the string instead of one of each. The string can then span as many lines as you want: >>>
'one
... two
......three'.
'one\ntwo\nthree

Notice that the string Python creates contains an in sequence everywhere our input started a new line. As programmers, we see escape sequences in strings. In Section 4.4, Printing Information, on page 70, we show that when strings are printed, users see the properly rendered strings rather than the escape sequences. That is, they see a tab or a quote rather than \t or '.


Normalizing Line Endings In reality, each of the three major operating systems use a different set of characters to indicate the end of a line. This set of characters is called a newline. On Linux and Mac OS X, a newline is one 'n' character; on version 9 and earlier of Mac OS, it is one V and on Windows, the ends of lines are marked with both characters as 'n'. Python always uses a single In to indicate a new line, even on operating systems like Windows that do things other ways. This is called normalizing the string: Python does this so that you can write exactly the same program no matter what kind of machine you're running on.


Printing Information
In Section 3.7. Writing and Running a Program, on page 59, built-in function print was used to print values to the screen. We will use print to print messages





to the users of our program expressions produce and the van examples of printing:

>>> print(1 + 1)
our program. Those messages may include the VA ons produce and the values that the variable refers to leve
>>> print("The Latin 'Oryctolagus The Latin 'Oryctolagus cum
in 'Oryctolagus cuniestus' and 'omestieri
cuniculus' means 'domestic rabbit
print doesn't allow any styling of the output: no colors, or o boldface. All output is plain text.

The first function call does what you would expect from would expect from the numeric examples have seen previously, but the second does something they were from previous string examples: it strips off the quotes around the strong and show us the string's contents rather than its representation. This cante makes the difference between the two even clearer:
>>> print('In 1859, Charles Darwin revolutionized biology
In 1859, Charles Darwin revolutionized biology
>>> print('and our understanding of ourselves and our understanding of ourselves
>>> print('by publishing "On the Origin of Species". by publishing "On the Origin of Species".
And the following the example shows that when Python prints a string, print the values of any escape sequences in the string rather than their backslashes representations:
>>> print('one\ttwo\nthree\tfour') one two three four

The example above shows how the tab character can be used to lay values out in columns.
In Section 4.3, Creating a Multiline String, on page 70. we saw that indicates a new line in multiline strings. When a multiline string is printed, then sequences are displayed as new lines:
>>> numbers = '
''one
 ... two
 ... three''
>>> numbers 'one\ntwo\nthree'
>>> print (numbers)
one
Two
 three

No comments:

Post a Comment