alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Python - Things to know before proceed - Input, Output Function and Formatting Tutorial

Input function

The input function is used to take user input at any point of time in the program using input().

#Taking input from user
name = input("Enter your name: ")
#Printing the input
print(name)
print(type(name))

Output:-

Enter your name: 123
<class 'str'>

The input function always returns a string. We can convert the return value of an input to any other data type explicitly.

To convert input to integer or float, we need to use Explicit Type Casting i.e int(), float() ,etc

#Taking input from user using int() as an explicit typecasting
name = int(input("Enter your name: "))
print(name)
#On taking input as 123, then it will provide datatype as int, instead of string
print(type(name))

Output:-

Enter your name: 123
<class 'int'>

To Take input without type Casting, we can use eval() instead of int(), float(), etc

Eval function will automatically detect data type of a user input

#Taking input from user using eval()
name = eval(input("Enter your name: "))
print(name)
#On taking input as 123.32, then it will provide datatype as float, instead of string
print(type(name))

Output:-

Enter your name: 123.32
<class 'float'>

 

Output function

The output function is used to display output on a console using print().

Syntax for print():-

print(value(s), sep= ‘ ‘, end = ‘\n’) 
#also include parameter like file and flush

Parameter:-

value (s) - can use any number of values, and will be converted to a string before printing

sep - optional parameter, It specifies how to separate multiple values, by default it is space.

end - optional parameter, It specifies what to print at end of the string, by default it is '\n'

Example1-

print('Fresherbell')
print('Fresherbell','Python','Tutorial')
print('Fresherbell','Python','Tutorial', sep="-")
print('Fresherbell','Python','Tutorial', sep="-",end='++')

 Output-

Fresherbell
Fresherbell Python Tutorial
Fresherbell-Python-Tutorial
Fresherbell-Python-Tutorial**

Example2-

a=21
b=22
c=23
d=35
e=50
print(c,e,a,end="++")
print(b,d,c,sep="\n",end='&&')
print(e,a,sep="--",end="--")
print(b,d,d,sep="==",end="fresherbell")

Output-

23 50 21++22
35
23&&50--21--22==35==35fresherbell

 

Output Formatting

Using String Literals

We can format the output using string literals, by starting a string with f or F before opening the quotation.

Inside quotation, we can write refer variable inside { }.

a="Python"
b="Fresherbell"
print(f'Hello {a}')
print(F'Hello {b}')

Output;-

Hello Python
Hello Fresherbell

Using format function

Formatting output can also be done using the format function, here { } curly braces in an expression are used as a placeholder, and .format() at last will hold the variable in the same order of curly braces by default.

We can also modify the order of a variable in an expression by using indexing starting from 0.

a="Python"
b="Fresherbell"
# By Default Order
print('Hello {}, Hello {}'.format(a,b))
# We can also change the order using indexing
print('Hello {1}, Hello {0}'.format(a,b))

Output:-

Hello Python, Hello Fresherbell
Hello Fresherbell, Hello Python

Using % Operator

In this % Operator with datatype symbol(i.e %d, %f, %s, etc) is used in an expression, and its associated variable is declared at last with % operator in the same order.

More than one argument has to be used inside the tuple. A single argument can be used directly.

a="Python"
b="Fresherbell"
c=3.15

# Single argument
print("c = %f" %c)
# More than one argument has to be used in tuple
print("Hello %s, Hello %s" %(a,b))

Output:-

c = 3.150000
Hello Python, Hello Fresherbell

 

Python

Python

  • Introduction
  • Installation and running a first python program
    • How to install Python ?
    • Running 1st Hello World Python Program
    • How to install Pycharm ?
  • Things to know before proceed
    • Escape Characters/Special Characters
    • Syntax ( Indentation, Comments )
    • Variable
    • Datatype
    • Keyword
    • Literals
    • Operator
    • Precedence & Associativity Of Operator
    • Identifiers
    • Ascii, Binary, Octal, Hexadecimal
    • TypeCasting
    • Input, Output Function and Formatting
  • Decision control and looping statement
    • if-else
    • for loop
    • While loop
    • Break Statement
    • Continue Statement
    • Pass Statement
  • Datatype
    • Number
    • List
    • Tuple
    • Dictionary
    • String
    • Set
    • None & Boolean
  • String Method
    • capitalize()
    • upper()
    • lower()
    • swapcase()
    • casefold()
    • count()
    • center()
    • endswith()
    • split()
    • rsplit()
    • title()
    • strip()
    • lstrip()
    • rstrip()
    • find()
    • index()
    • format()
    • encode()
    • expandtabs()
    • format_map()
    • join()
    • ljust()
    • rjust()
    • maketrans()
    • partition()
    • rpartition()
    • translate()
    • splitlines()
    • zfill()
    • isalpha()
    • isalnum()
    • isdecimal()
    • isdigit()
    • isidentifier()
    • islower()
    • isupper()
    • isnumeric()
    • isprintable()
    • isspace()
    • istitle()
  • Python Function
    • Introduction
  • Lambda Function
    • Lambda Function
  • Python Advanced
    • Iterators
    • Module
    • File Handling
    • Exceptional Handling
    • RegEx - Regular Expression
    • Numpy
    • Pandas

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.

Toggle