Sunday, April 30, 2023

PYTHON(DAY4) Input and Output Funcions

PYTHON(DAY4)

Input and Output in python

INPUT FUNCTION

How to Take Input from User in Python

Syntax:

input('xyz')

where xyz is an optional string that is displayed on the string at the time of taking input.

Example 1: Getting input from user

x = input('Enter your Name:')
print(x)

here, we get the output as:

Enter your Name: xyz

type() function:

Example 2: using type function

y = 'hi'
print(type(y))

here, we get the output as:

<class ‘str’>

Python takes all the input as a string input by default. To convert it to any other data type we have to convert the input explicitly. For example, to convert the input to int or float we have to use the int() and float() method respectively.

How to take integer as an input

Example 3: using int() function

x = int(input('enter a number:'))
print(x)

here, we get the output as:

enter a number: 10

How to take Multiple Inputs from user

  • Using split() method

Syntax:

input().split(separator, maxsplit)

Parameters

The separator parameter breaks the input by the specified separator. By default, whitespace is the specified separator.

The split() method is used to split the Python string, but we can use it to get the multiple values too.

a,b,c = input('enter three values:')
print('enter first name:',a)
print('enter last name:',b)
print('enter school name:',c)

here, we get output as:

enter three values: raj kumar nirmal
enter first name: raj
enter last name: kumar
enter school name: nirmal

OUTPUT FUNCTION

Example:

print('hi')
print('hello')

here, we get output as:

hi
hello

what if we use “end” parameter in print statement, let see the difference between the above print statement and this statement

By default end takes space to print the outputs in single statement seperated by space

print('hello', end = ' ')
print('world)

here, we get output as:

hello world

No comments:

Post a Comment

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...