Thứ Năm, Tháng Ba 6, 2025
spot_img
HomePrint () function in python - quantrimang.com

Print () function in python – quantrimang.com

Print in Python Used very popular when developing software. Here are Things you need to know about how to use the Print command in Python.

Programming is currently a industry that many people pursue when information technology is growing. Companies currently have great demand in recruiting highly skilled programmers to develop products. Learning programming is complicated but not necessarily too difficult. Start with the most common languages ​​and Python is one of them.

If you are like most Python users, then you probably started your Python journey by learning about print (). It has helped most users write their own Hello World command line. You can use it to display the notifications that have been formatted on the screen and easily detect some common errors. But if you think that is all that you need to know about Python's Print () function, then you missed a lot!

Continue to read to make the most of this boring and not appreciated jaw. This tutorial will help you quickly use Python Print () effectively. However, prepare for a deeper understanding when you look through the sections. You may be surprised about what Print () brings! Print () is not just a print command as its name.

Print function () In Python, there is the effect of displaying magic to the screen when the program is executed.

Full syntax of print ():

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Parameters of the print function ():

  • objects: The object is printed, there may be many objects. Will be converted into a chain before displaying the screen.
  • sep: How to separate objects, the default value is a space .
  • end: The final value is printed on the screen.
  • file: By default the Print function will write the content in the file Sys.stdout.
  • flush: The valuable value is False.

How to operate of the Print () function in Python?

You can transfer variables, string, numbers or other data types as one or more parameters when using the Print () function in Python. After that, these parameters displayed in the form of the corresponding string function. To create a series of results, the conversion sequences are connected by the distance between them.

This example code transfers two name parameters and age to the Print function.

name = "Alice"
age = 25
print("Hello, my name is", name, "and I am", age, "years old.")

Result:

Hello, my name is Alice and I am 25 years old.

Note: SEP, End, File and Flush are keyword parameters. If you want to use the SEP parameter, you must use this:

print(*objects, sep = 'separator')

Not used:

print(*objects, 'separator')

Example 1: How to print () works in Python

print("Học Python rất thú vị.")

a = 5
# 2 object
print("a =", a)

b = a
# 3 object
print('a=", a, "= b')

Running the program, the return results are:

Học Python rất thú vị.
a = 5
a = 5 = b

In the three statements in the example above, only the Object parameter is used in statements.

Example 2: print () with parameters Separator and End

a = 5
print("a =", a, sep='00000', end='\n\n\n')
print("a =", a, sep='0', end='')

Running the program, the return results are:

a =000005

a =05

Example 3: print () with file parameters

In Python, you can Print Objects with the file by determining the file parameter.

sourceFile = open('python.txt', 'w')
print('Pretty cool, huh!', file = sourceFile)
sourceFile.close()

This program tries to open python.txt in writing mode. If this file does not exist, the file python.txt Created and opened in recording mode.

Here, for example, the file has been transferred sourcefile to file parameters. String object (string) “Pretty Cool, Huh!” printed to python.txt file (check it in the system).

Finally, this file is closed by Close () method.

How to direct the output of Print () into a file in Python

Most people want to print the results to the standard or command line format. However, when you want to transfer the results directly to the existing file, what to do?

Suppose you have a text file and want to add a little text with the Print () function. To open & write files in Python, call Open () function. Inside it, you include file name, output.txt in this case, and add -W mode, meaning only writing.

In this mode, every time you run the file code of the file will be deleted and replaced with any new text you add. If you do not want to lose content, instead, you can use -A mode, to add text to the end of the file.

Inside the Print () function, you can add any desired content to the file and place the file parameter with the Placeholder's name that created the file to add to the text, in this example is f.

with open('output.txt', 'w') as f:
print('Hello World!', file=f)

Hopefully through this article, you already know Print in python is and how to use them. In addition, you can find out: Python functions are built

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments