Python Print() Statement: Hur man skriver ut med exempel

โšก Smart sammanfattning

Python print() function displays a message or object on the screen after converting it to a string. The examples below show how to print strings, blank lines, and multiple items, and how to control line endings.

  • ๐Ÿ–จ๏ธ Syfte: The print() function outputs strings and objects to the screen.
  • ๐Ÿ”ค Syntax: Call print(object(s)) with the value you want to display.
  • ๐Ÿ” Multiple Prints: Each print() call outputs on its own new line by default.
  • โฌœ Blank Lines: Use โ€œ\nโ€ or multiply it to print empty lines.
  • โžก๏ธ end Parameter: Change the line ending, for example end=โ€™ โ€˜, in Python 3.
  • โš ๏ธ Version Note: Python 3 uses print() as a function; Python 2 used it as a statement.

Python Print() Statement

Python skriv ut () -funktionen

Print()-funktionen i Python anvรคnds fรถr att skriva ut ett angivet meddelande pรฅ skรคrmen. Skriv ut kommandot i Python skriver ut strรคngar eller objekt som omvandlas till en strรคng vid utskrift pรฅ en skรคrm.

Syntax:

print(object(s))

Hur man skriver ut en enkel strรคng i Python?

Oftare behรถver du inte Skriv ut strรคngar i din kodningskonstruktion.

Sรฅ hรคr skriver du ut ett uttalande Python 3:

Exempel: 1

Fรถr att skriva ut Vรคlkommen till Guru99, anvรคnd Python skriv ut uttalande enligt fรถljande:

print ("Welcome to Guru99")

Produktion:

Vรคlkommen till Guru99

In Python 2, samma exempel kommer att se ut

print "Welcome to Guru99"

Exempel 2:

Om du vill skriva ut namnet pรฅ fem lรคnder kan du skriva:

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

Produktion:

USA
Canada
Germany
France
Japan

Hur man skriver ut tomma rader

Ibland behรถver du skriva ut en tom rad i din Python programmera. Fรถljande รคr ett exempel att utfรถra denna uppgift med Python utskriftsformat.

Exempel:

Lรฅt oss skriva ut 8 tomma rader. Du kan skriva:

print (8 * "\n")

Eller:

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

Hรคr รคr koden

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

Produktion

Welcome to Guru99







Welcome to Guru99

Skriv ut kommandot avsluta

By default, the 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 only in Python 3 +.

Exempel 1:

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

Produktion:

Vรคlkommen till Guru99!

Exempel 2:

# ends the output with '@'
print("Python", end='@')

Produktion:

Python@

Vanliga frรฅgor

In Python 2, print was a statement written as print โ€œtextโ€. In Python 3, print is a function called as print(โ€œtextโ€) with parentheses. Python 3 is the current standard, so always use the function form.

The sep parameter sets the separator placed between multiple items in one print() call. By default it is a space. For example, print(โ€œaโ€, โ€œbโ€, sep=โ€-โ€œ) outputs a-b instead of a b.

Use an f-string, such as print(fโ€Hello {name}โ€), or separate items with commas, such as print(โ€œHelloโ€, name). F-strings are the modern, readable way to mix text and variables in Python 3.6 och senare.

Yes. AI assistants can spot missing parentheses, wrong quotes, or Python 2 syntax, and explain the exact error. They also suggest cleaner ways to format output, which helps beginners fix print-related mistakes quickly.

print() is a simple way to inspect data, model outputs, and intermediate values while building AI programs. Developers use it to trace how data flows through code, though logging libraries are preferred for larger projects.

Sammanfatta detta inlรคgg med: