📜  python print new line - Python (1)

📅  最后修改于: 2023-12-03 15:34:03.727000             🧑  作者: Mango

Python Print New Line

When programming in Python, it is common to print output to the console. Sometimes, you may want to print a new line character in your output. In this guide, we will explore how to do this in Python.

Using the print() function

In Python, the print() function is used to output text to the console. By default, this function will not print a new line character at the end of the output. To print a new line, you can add the special character '\n' to the end of your string.

print("Hello, World!\n")

This will print the text "Hello, World!" to the console, followed by a new line character.

Using the end parameter

Another way to print a new line character in Python is to use the end parameter of the print() function. By default, this parameter is set to '\n', which means that a new line character will be printed at the end of the output. However, you can change this parameter to any other value that you want, including an empty string ('') or a space (' ').

# Print "Hello, World!" with a new line character using the 'end' parameter
print("Hello, World!", end='\n')

# Print "Hello, World!" without a new line character
print("Hello, World!", end='')
Conclusion

Printing a new line character in Python is easy and can be done using either the '\n' character or the end parameter of the print() function. Understanding how to do this is essential when working with console output in Python.