Python InputOutput
Python InputOutput
Topperworld.in
Input/Output
where prompt is an optional string that is displayed on the string at the time
of taking input.
Example:
# Output
print("Hello, " + name)
print(type(name))
Output:
©Topperworld
Python Programming
Example:
Output:
Take the elements of the List/Set one by one and use the append() method in
the case of List, and add() method in the case of a Set, to add the elements to
the List / Set.
Example:
List = list()
Set = set()
l = int(input("Enter the size of the List : "))
s = int(input("Enter the size of the Set : "))
print("Enter the List elements : ")
for i in range(0, l):
©Topperworld
Python Programming
List.append(int(input()))
print("Enter the Set elements : ")
for i in range(0, s):
Set.add(int(input()))
print(List)
print(Set)
Output:
©Topperworld
Python Programming
➢ Parameters:
• value(s) : Any value, and as many as you like. Will be converted to string
before printed
• sep=’separator’ : (Optional) Specify how to separate the objects, if there is
more than one.Default :’ ‘
• end=’end’: (Optional) Specify what to print at the end.Default : ‘\n’
• file : (Optional) An object with a write method. Default :sys.stdout
• flush : (Optional) A Boolean, specifying if the output is flushed (True) or
buffered (False). Default: False
Example:
# Python program to demonstrate
# print() method
print("Topper World")
Output:
Topper World
❖ Formatting Output
Formatting output in Python can be done in many ways. Let’s discuss them
below:
©Topperworld
Python Programming
# Declaring a variable
name = "Topper World"
# Output
print(f'Hello {name}! How are you?')
Output:
➢ Using format()
We can also use format() function to format our output to make it look
presentable. The curly braces { } work as placeholders. We can specify the
order in which variables occur in the output.
➢ Using % Operator
We can use ‘%’ operator. % values are replaced with zero or more value of
elements. The formatting using % is similar to that of ‘printf’ in the C
programming language.
⚫ %d – integer
⚫ %f – float
⚫ %s – string
⚫ %x – hexadecimal
⚫ %o – octal
©Topperworld
Python Programming
Example:
add = num + 5
# Output
print("The sum is %d" %add)
Output:
Enter a value: 50
The sum is 55
©Topperworld