Mastering Python

  • Mastering Python
  • Overview of Windows Operating System
  • Setup Ubuntu VM on GCP
  • Setup Postgres Database
  • Perform Database Operations
  • Getting Started with Python
  • Basic Programming Constructs
  • Pre-defined Functions
    • Overview of Pre-defined Functions
    • Numeric Functions
    • Overview of Strings
    • String Manipulation Functions
    • Formatting Strings
    • Print and Input Functions
    • Date Manipulation Functions
    • Special Functions
    • Exercises
  • User Defined Functions
  • Overview of Collections - list and set
  • Overview of Collections - dict and tuple
  • Manipulating Collections using Loops
  • Development of Map Reduce APIs
  • Understanding Python Map Reduce Libraries
  • Overview of Object Oriented Programming
  • Overview of Pandas Libraries
  • Web Scraping using Beautiful Soup
  • Database Programming – CRUD Operations
  • Database Programming – Batch Operations
Subscribe to our Newsletter

Print and Input Functions¶

Let us understand details related to print and input functions. These functions will help us in enhancing our learning experience.

  • print is primarily used to print the strings on the console while input is used to accept the input from console.

  • input will typically prompts the user to enter the value and we can assign to a variable and use it further.

  • By default, the value passed for input is of type str and hence we need to convert the data type of the values entered before processing.

input?
Signature: input(prompt='')
Docstring:
Forward raw_input to frontends

Raises
------
StdinNotImplentedError if active frontend doesn't support stdin.
File:      /opt/anaconda3/envs/beakerx/lib/python3.6/site-packages/ipykernel/kernelbase.py
Type:      method
a = int(input('Enter first value: '))
b = int(input('Enter second value: '))
print(f'Sum of first value {a} and second value {b} is {a + b}')
Enter first value:  4
Enter second value:  5
Sum of first value 4 and second value 5 is 9
  • When we use print with one string, by default it will have new line character at the end of the srring.

  • We can also pass multiple strings (varrying arguments) and space will be used as delimiter

  • We can change the end of the string character, by passing argument

  • print can accept 2 keyword arguments sep and end to support the above mentioned features.

You will understand more about keyword arguments and varrying arguments in User Defined Functions.

print?
# Default print by using one string as argument
print('Hello')
print('World')
Hello
World
# Default print by using multiple arguments. Each argument is of type string
print('Hello', 'World')
Hello World
# Changing end of line character - empty string
print('Hello', end='')
print('World')
HelloWorld
# Changing separater to empty string
print('Hello', 'World', sep='')
HelloWorld
print('Hello', 'World', sep=', ', end=' - ')
print('How', 'are', 'you?')
Hello, World - How are you?
Formatting Strings Date Manipulation Functions

By Durga Gadiraju
© Copyright ITVersity, Inc.