Input and Output in Python
Input and Output in Python
INPUT
1# Taking input from the user
# Output
print("Hello, " + name)
print(type(name))
Output:
Enter your name: GFG
Hello, GFG
<class 'str'>
add = num + 1
# Output
print(add)
Output:
Enter a number: 25
26
OUTPUT
Output
GFG
G F G
Example: Python Print output with custom sep and end parameter
Output
GFG@G#F#G
Formatting Output
We can use formatted string literals, by starting a string with f or F before opening quotation marks or
triple quotation marks. In this string, we can write Python expressions between { and } that can refer to
a variable or any literal value.
Example: Python String formatting using F string
# Declaring a variable
name = "Gfg"
# Output
print(f'Hello {name}! How are you?')
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.
# Initializing variables
a = 20
b = 10
# addition
sum = a + b
# subtraction
sub = a- b
# Output
print('The value of a is {} and b is {}'.format(a,b))
value_b = b,
sub_value = sub))
Output:
The value of a is 20 and b is 10
30 is the sum of 20 and 10
10 is the subtraction of 20 and 10
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
Example:
add = num + 5
# Output
print("The sum is %d" %add)
Output:
Enter a value: 50
The sum is 55