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

Input_Output.ipynb - Colaboratory

The document provides an overview of the print function in Python, detailing its syntax and parameters, along with examples of its usage. It also covers the input function, the eval function, and the center method for string formatting. Additionally, it explains formatted strings and includes examples for better understanding.

Uploaded by

Shreya Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Input_Output.ipynb - Colaboratory

The document provides an overview of the print function in Python, detailing its syntax and parameters, along with examples of its usage. It also covers the input function, the eval function, and the center method for string formatting. Additionally, it explains formatted strings and includes examples for better understanding.

Uploaded by

Shreya Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

18/11/2023, 19:20 Input_Output.

ipynb - Colaboratory

print() function
It prints the message to the screen or any other standard output device.

Syntax:

print(value(s), sep= ' ', end = '\n', file=file, flush=flush)

Parameters:

value(s): Any value, and as many as you like. Will be converted to a 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) Specfies where we want to route the output. By default it is routed to the console. Default: sys.stdout

flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False

print('Hello', 'World', sep =',')

print('Hello','World', sep = ' ')

account_circle Hello,World
Hello World

print('Hello World', end = ' ') #whitespace


print('This is a Python code')

print('Hello World', end = '\t') #tab


print('This is a Python code')

print('Hello World', end = '\r') #replace


print('This is a Python code')

print('Hello World', end = '\b') #backslash


print('This is a Python code')

print('C1', end = '\\')


print('Users', end = '\\')
print('\n')

print('C1', end = '/')


print('Users', end = '/')

Newfile= open("exam_score.txt", "w")

# variables
exam_name = "Python"
exam_date = "2-Sept"
exam_score = 400

print(exam_name, exam_date, exam_score, file=Newfile , sep = ",")

# close the file


Newfile.close()

for i in range(10):
print(i, flush =False)

from time import sleep


for i in range(10):
print(i, flush =True)
sleep(0.5)

print(formatted string)
The output can be formatted using the special symbol %
print(“formatted string” % (variables list))

https://colab.research.google.com/drive/16sJfEDATPW0mZHxwhv9LRhc1lz7adsRG#printMode=true 1/3
18/11/2023, 19:20 Input_Output.ipynb - Colaboratory
The statement begins with a format string consisting of a sequence of character and conversion specifications.

Format Symbol-Purpose

%c character

%s string

%d or %i Signed decimal integer

%u Unsigned decimal integer

%o Octal integer

%x or %X Hexadecimal integer

%e or %E Exponential Notation

%f Floating point number

x,y = 10, 15
print('x = %i, y = %d' %(x,y))
print('%d, %d'%(y,x))

x = 10, y = 15
15, 10

n = 'abc'
print(' %c, %c %s' % (n[0], n[1], n))

a, b abc

input
It reads a string from the user's keyboard. (reads and returns an entire line of input)

It gives you a string so must cast if working with numbers

name = input("Hi, What's your name?")


print(name)

Hi, What's your name?Abc


Abc

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


print(5*num)

Type a number :3
15

#accepting a list of strings

list1 = [x for x in input("Enter strings: ").split()]


print(list1)

Enter strings: Abc xyz


['Abc', 'xyz']

#eval() function takes a string and evaluates the result of the string by taking it as a python expression
a,b = 10,20
result = eval('a+b-10')
print(result)

20

#using eval() with input()


x = eval(input('Enter an Expression: '))
print("Result = %d"%x)

Enter an Expression: 2+4*3


Result = 14

center() method is an inbuilt function that is used to align the string to the center by filling paddings to the left and right of the string.

https://colab.research.google.com/drive/16sJfEDATPW0mZHxwhv9LRhc1lz7adsRG#printMode=true 2/3
18/11/2023, 19:20 Input_Output.ipynb - Colaboratory
String.center(length, fillchar)

str1 = 'Hi'
print(str1.center(6))
print(str1.center(6,"@"))

Hi
@@Hi@@

https://colab.research.google.com/drive/16sJfEDATPW0mZHxwhv9LRhc1lz7adsRG#printMode=true 3/3

You might also like