Because of this, the two statements were printed on the same line, separated only by a single space. In the example above, we modified the end= parameter to simply be a space character. For example, if we wanted to keep printing multiple statements on the same output line, we could simply write: # Printing in Python Without Newlines We can actually change the ending of these print statements by passing in a different string. The reason that these statements print across different lines is because the print() function has a parameter end= which defaults to the newline character, '\n'. Take a look at the code below as an example: # Printing Multiple Statements in Python When you print multiple statements in Python, by default they print across multiple lines. We can see that the escape character isn’t printed, but that the newline character is printed in full. # Welcome to datagy!\nLearn Python and Data Science! Let’s see what this looks like: # Escaping the Newline Character in Python By prefixing this to the newline character, the newline character will be escaped. We can see that by including the \n newline character, that when the string was printed the text was split across multiple lines.Īs a fun aside, you may be thinking, “what if I actually want to print '\n'?” Thankfully, this is very simple to do! The \ character acts as the escape character. String = "Welcome to datagy!\nLearn Python and Data Science!" Let’s take a look at an example of how you can use the Python newline character, \n, to print on multiple lines in Python: # Printing on Multiple Lines in Python In essence, this character indicates where a line ends – any further characters will be printed on a new line. Wherever this character is inserted into a string, a new line will be created when the string is printed out. In Python, the character to create a new line is the \n character.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |