Py12: Output in Python

Introduction

The print() function is used to display a text on the screen or send message to output devices. A string is passed to the function which then the string get displayed on the screen.

Syntax

print(object, sep = separator, end = end, file = file, flush = flush)

What are the meaning of the parameters:

object
is passed to be displayed on screen. There can be more than one object separated using comma. It is converted to string before doing that.
seperator
(optional) is a character used to seperate each object. The default is ''
end
(optional) to specifies what to print at the end. The default is '\n'
file
(optional) file is object with write method. The default value is sys.stdout
flush
(optional) this is boolean value. True if output is flushed or False buffered. The default is False.

Display strings

Example 1

print("Hello World")

Output

Hello World

Example 2

print("Hello","World") # Hello World

Output

Hello World

Note that after display the "Hello", the print() function automatically puts a space before printing "World".

Seperator

Example 3

print("15", "10","2023", sep = "-")

Output

15-10-2023

Newline and end parameter

to print the string in the next line we can use the Newline character '\n' in the string.

Example 4

print("Hello\nWorld") # Hello World

Output

Hello
World

Example 5

print("one","two", end = " $$$")

Output

 one two $$$

the print() automatically put's '\n' at the end of each line after printing it.

Example 6

print("one","two")
print("three", "four")

Output

one two
three four

Sometimes, we want to prints the following output in same line. To do that we need to set the end parameter to to single space enclosed in quote sign " ".

Example 7

print("one","two", end = " ")
print("three", "four") 

Output

one two three four

Formatting string

To create a string dynamically by adding other informations usually stored in variable to be used as output. This can be achived simply by using commas and list out the object in sequentially in the order we want to display.

Example 8

a = 100
b = 200

print("value", a, "and", b)

Output

value 100 and 200

Using the 'f'

The string to be formatted is prefixed with the 'f' letter. Inside the string we specify the parameter by mentioning the variable name inside curly brackets. We place the parameter at the desired location.

Example 9

print(f"value {a} and {b}")

or, by storing the result of the formatted string in a variable.

Output

value 100 and 200

Example 10

a = 100
b = 200

msg = f"value {a} and {b}"

print(msg)

Output

value 100 and 200

Using the format() method

String uses the dot operator to access the format() method while sending the parameters. Curly bracket are used to indicate the positions to display the values.

Example 11

print("value {} and {}".format(a,b))

Output

value 100 and 200

format() method with indexes

We can also specify the index of the parameter in the curly bracket. The index start with 0, 1 and so on.

Example 12

print("value {0} and {1}".format(a,b))
print("value {1} and {0}".format(a,b))

Output

value 100 and 200
value 200 and 100

format() method with keyword

The format() method also allows programmer to use keyword to set the parameters, and then be used in the string to be formatted.

Example 13

print("Name {name} age {age}".format(name="civa", age="39"))

Output

Name civa age 39

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net