Home
About Us Privacy Policy
 

print statement

ADVERTISEMENT

Beginner Print



The print statement is an in-built method in python to send textual data to the standard output, i.e., terminal or command prompt.

The primary usage of the print statement is straightforward.

# print statement
print("Hello, Python!")

Hello, Python!

You can use the print statement for a variety of different purposes.

  • logging of events
  • printing text in a particular format

my_first_name = "John"
my_last_name = "Doe"
my_full_name = my_first_name + " " + my_last_name
print("My name is " + my_first_name + ", " + my_full_name)

My name is John, John Doe

You can separate multiple arguments using a comma(,) when using the print statement.

print("John", "Doe")

John Doe


ADVERTISEMENT

sep argument

You can also join multiple arguments using a separator. To do so, use the sep argument at the end of the print statement. We haven't covered functions and parameters yet, but it will become evident once we go over them. For now, let's follow the example below.

print("John", "Doe", "johndoe", 46, "21/01/1974", sep=':')

John:Doe:johnndoe:46:21/01/1974


ADVERTISEMENT

Whitespace characters

Before discussing the end argument, we need to know a bit about the whitespace characters. Whitespace characters are not visible to the end-user but do perform a specific function.

The following are whitespace characters:

Newline or "\n"

Newline character is equivalent to pressing the "enter" key while editing a document. It simply moves the cursor to the beginning of the following line.

print("1\n2\n3")

1
2
3


Tab or "\t"

"\t" does what pressing the "Tab" key does. It just simply moves the cursor to the next Tab stop.

print("1\t2\t3")

1    2    3


ADVERTISEMENT

end argument

If you might have noticed, the print statement always prints a newline after printing each of the arguments. Sometimes, we want to avoid or emit some other character in place of the default newline.

To do so, pass the intended value to the end argument.

print("Hello, ", end="")
print("World!")

Hello, World!


Conclusion

In this chapter, we learned the usage and importance of the print statement, along with some of its arguments.


ADVERTISEMENT



All product names, logos, and brands are property of their respective owners.