Line break in python | Laptrinhcanban.com

HOME › >>

Line break in python

Posted at: https://laptrinhcanban.com/en

Instructions on how to break line in python. You will learn how to line breaks outside of python statements using the line break character in python, how to line break inside a python statement and write it on multiple lines, how to print a line break in python, and how to print No line breaks in python in this lesson.

Line breaks outside of python statements

Basically, a statement in python is written on a single line and is terminated using the newline character generated when you press the ENTER key. This is very different from other languages ​​such as JavaScript, which can arbitrarily break the line created by the ENTER key when writing commands.

Once the command has finished and you are out of the way, you can optionally break the line by pressing the ENTER key while writing your python code for good visibility. These gaps will also be ignored when the program is processed.
For example, we can line breaks outside of an arbitrary python statement like so:

str1 = "Hello"

str2 =", Việt Nam"



print (str1 + str2)

The result of the above example is the same as the following:

str1 = "Hello"
str2 =", Việt Nam"
print (str1 + str2)

Result

Hello, Việt Nam

Line break inside python statement

You cannot line breaks inside a python statement just by pressing ENTER

As we all saw above, a statement in python is written on a single line and is terminated with a newline generated when you press ENTER.
Therefore, in a statement that is too long, if you want a line break in the statement and write the statement on multiple lines so that it is easy to see, you cannot simply break the line just by pressing the key ENTER.
Python will treat that statement to end at the press ENTER key and ignore the rest of the statement, causing the command to crash at runtime.
For example the following statement:

num = 10 + 20 + 30 + 40 + 50 + 60 + 70 
print (num)

Assuming you want a line break after the character 40 +, if you break the line by pressing ENTER, the error will occur as follows:

num = 10 + 20 + 30 + 40 +
50 + 60 + 70
print (num)

Error SyntaxError was returned:

  File "Main.py", line 1
num = 10 + 20 + 30 + 40 +
^
SyntaxError: invalid syntax

So you cannot simply press ENTER to break the line between statements in python.

Use the backslash\ to return a line within the python statement

To return a line within a python statement and write the statement on multiple lines, prepend the backslash \ where you want the line break in the statement.

The written syntax will look like this:

abc \
xyz

In it abc and xyz are the parts of the statement that you want to write in the middle of the line.
A \ carriage return sign tells python that you want a line break within the python statement and write the statement on multiple lines , so python will not end the statement at this position but continue to read the concatenation at the next line below.

With the above error example, we need to rewrite it with the \ following sign :

num = 10 + 20 + 30 + 40 + \
50 + 60 + 70
print (num)

Try this with interactive mode :

Line break in python

Pay attention to the yen character, which is the sign \ represented in windows. And please pay attention to the ellipses ..., it is because python \ has identified the statement is still continuing after processing , so you can write the above statement.

From the example above, you can see that using the diacritic \has helped us to line up in the middle of a statement that is too long to continue writing and still works.

In python, we use a function print to print characters to the screen.
There are many ways to use the print function , in which the basic syntax of the print function does not specify an option as follows:

print(line)

line is the output line you want to print to the screen. You can either directly specify the value of the line or assign it to a variable and print the value of that variable.

By default, after finishing a statement using the basic print function , the output displayed on the screen will automatically print a line break in python
So you don’t need to worry about the resulting line being printed down the line in python. when using the basic print function or not.

Let’s see the following specific example:

# chỉ định trực tiếp dòng kết quả muốn in ra màn hình
print("Hello")
print("Viet Nam")
print("Hello Viet Nam")

# gán biến và in ra màn hình
xinchao = "Hello"
tennuoc= "Viet Nam"
print(xinchao)
print(tennuoc)
print(xinchao + tennuoc)

Try both of the above examples in python interactive mode , you can see they both give the same result, as well as the results automatically print newline in python :

>>> print("Hello")
Hello
>>> print("Viet Nam")
Viet Nam
>>> print("Hello Viet Nam")
Hello Viet Nam

>>> xinchao = "Hello"
>>> tennuoc= "Viet Nam"
>>> print(xinchao)
Hello
>>> print(tennuoc)
Viet Nam
>>> print(xinchao + tennuoc)
Hello Viet Nam

Print line breaks in python

In the above section we know. By default, after finishing a statement using the basic print function without option, the results displayed on the screen will automatically print down the line in python .
The question is what should we do to print without newline in python ?
The way to do it is very simple, we need to add an option by specifying parameters end in the print function as follows:

print(line, end='')

By adding a parameter end='' to the print function as above, the results will be printed without a line break in python like the following example:

# Print no line break in python
print("Welcom to ", end='')
print("Việt Nam")

Results of printing without line breaks in python:

Welcom to Việt Nam

Summary

Above, Kiyoshi showed you how to line break in python. To better understand the lesson content, practice rewriting today’s examples.

And let’s learn more about Python in the next lessons.

URL Link

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/kien-thuc-can-ban-ve-chuong-trinh-python/xuong-dong-trong-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.