Python Print() Statement: Hvordan skrive ut med eksempler

Python print () -funksjon

The print() function in Python brukes til รฅ skrive ut en spesifisert melding pรฅ skjermen. Skriv ut kommandoen i Python prints strings or objects which are converted to a string while printing on a screen.

syntax:

print(object(s))

How to Print a simple String in Python?

More often then not you require to Print strings in your coding construct.

Here is how to print statement in Python 3:

Example: 1

To print the Welcome to Guru99, use the Python print statement as follows:

print ("Welcome to Guru99")

Utgang:

Velkommen til Guru99

In Python 2, same example will look like

print "Welcome to Guru99"

Eksempel 2:

If you want to print the name of five countries, you can write:

print("USA")
print("Canada")
print("Germany")
print("France")
print("Japan")

Utgang:

USA
Canada
Germany
France
Japan

How to print blank lines

Sometimes you need to print one blank line in your Python program. Following is an example to perform this task using Python print format.

Eksempel:

Let us print 8 blank lines. You can type:

print (8 * "\n")

eller:

print ("\n\n\n\n\n\n\n\n\n")

Her er koden

print ("Welcome to Guru99")
print (8 * "\n")
print ("Welcome to Guru99")

Produksjon

Welcome to Guru99







Welcome to Guru99

Print end command

By default, print function in Python ends with a newline. This function comes with a parameter called โ€˜end.โ€™ The default value of this parameter is โ€˜\n,โ€™ i.e., the new line character. You can end a print statement with any character or string using this parameter. This is available in only in Python 3+

Eksempel 1:

print ("Welcome to", end = ' ') 
print ("Guru99", end = '!')

Utgang:

Velkommen til Guru99!

Eksempel 2:

# ends the output with โ€˜@.โ€™

print("Python" , end = '@')

Utgang:

Python@

Oppsummer dette innlegget med: