Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
68 views

Input and Output in Python

The document discusses input and output in Python. It shows how to take input from the user as a string using input() and as an integer using int(input()). It demonstrates different methods for formatting output, including print() with custom separators and ends, formatted string literals using f-strings, string formatting with format(), and using the % operator.

Uploaded by

Anoop A S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Input and Output in Python

The document discusses input and output in Python. It shows how to take input from the user as a string using input() and as an integer using int(input()). It demonstrates different methods for formatting output, including print() with custom separators and ends, formatted string literals using f-strings, string formatting with format(), and using the % operator.

Uploaded by

Anoop A S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Input and Output in Python

INPUT
1# Taking input from the user

name = input("Enter your name: ")

# Output
print("Hello, " + name)
print(type(name))

Output:
Enter your name: GFG
Hello, GFG
<class 'str'>

2# Taking input from the user as integer

num = int(input("Enter a number: "))

add = num + 1

# Output
print(add)

Output:
Enter a number: 25
26
OUTPUT

# Python program to demonstrate


# print() method
print("GFG")

# code for disabling the softspace feature


print('G', 'F', 'G')

Output

GFG
G F G

Example: Python Print output with custom sep and end parameter

# Python program to demonstrate


# print() method
print("GFG", end = "@")

# code for disabling the softspace


feature
print('G', 'F', 'G', sep="#")

Output
GFG@G#F#G

Formatting Output

Formatting output in Python can be done in many ways.

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.

Example: Python string formatting using format() function

# 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))

print('{2} is the sum of {0} and {1}'.format(a,b,sum))

print('{sub_value} is the subtraction of {value_a} and {value_b}'.format(value_a = a ,

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:

# Taking input from the user


num = int(input("Enter a value: "))

add = num + 5

# Output
print("The sum is %d" %add)

Output:
Enter a value: 50
The sum is 55

You might also like