User Input/Printing Statement/Block & Indentation

M3-R5.1 · Chapter 3: Introduction to Python · 7 min read

Taking User Input in python

  • In Python for taking user input , we use  input() function.
  • When input() is called, the program pauses and waits for the user to type something and press Enter. 
  • After reading data, it converts it into a string and returns that.

NOTE: The input() method reads the user's input and returns it as a string.

  Syntax: input (prompt)

     Prompt (Optional) –A string that is displayed to the user (usually on screen)  before input.

Example 1: Write a program to Takes the user input using the input() method.

                      >>> name = input()            #  suppose we type How are you?

                      >>> name                               OUTPUT:   'How are you?'

Example 2: Write a program to receive a user's name using input method and print with Hello .

name = input("What is your name? ")

print("Hello,", name)

Above, the input() function takes the user's input in the next single line, so whatever user writes in a single line would be assign to a variable name. So, the value of a name would be whatever user has typed.

Example 3: Write a program to get the name using Input() with optional prompt parameter. Show them

>>> name = input('Enter Your Name: ')

       Enter Your Name: karamraji Institute

>>> name

OUTPUT:'karamraji Institute'

In the above example, the input('Enter Your Name: ') function with prompt string 'Enter Your Name: '. So, in the next line it will display a prompt first, asking user what to do. User can then enter the name after the prompt string in the same line and that would be assign to the name variable.

The input() function converts all the user input to a string even if that's the number.

Example 4:  Write a program to Get user's age and add 5 in the given Age

age = int (input ("Enter your age: "))

print("Next year, you will be", age + 5)

Example 5: Write a program to Add two numbers

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

sum = num1 + num2

print("The sum is:", sum)

Example 6: T Write a program to Take multiple inputs in one line

x, y = input("Enter two numbers separated by space: ").split()

print("You entered:", x, "and", y)    Output: You entered: 20 and 30

z=x+y

print(z)             Output:  2030

Example 7: Write a program to Convert input to float

price = float(input("Enter the price: "))

print("You entered:", price)

Output: Enter the price: 25.35

               You entered: 25.35

Example 8: Write a program to Convert input to int and print square

val = input("Enter an integer: ")  

val = int(val) # casting into string  

sqr = (val*val) # getting square  

print("Square of the value:",sqr)  

Output:  Enter an integer: 12

                Square of the value: 144

Printing statements in python 

          In Python, printing statements is straightforward, to the built-in print() function. It is used to display information, output results, or debug programs.

     Note: In Python, we use the print() function to display output on the screen.

Basic Syntax :    The print() function outputs text or variables to the console:

          Example :    print("Hello, World!")        # Prints a simple string

Printing Variables or multiple value :   You can use the print() function to display the value of variables:

Example:

name = "Naitik"

age = 25

class1=”10th “

print("Name:", name)

print("Age:", age)

print("class:", class1)

 or

            print("Name:", name, "Age:", age, "Class:", class1)

Using sep (Separator) in print() :

By default, Python separates values with a space, but you can change this using the sep parameter.

Example: print("Naitik", "25", "10th ", sep=" | ")

Output:  Naitik | 25 | 10th

Using end in print() :  

By default, print() adds a newline (\n) at the end. You can change this using the end parameter.

Example:

print("Hello", end=" ")

print("World!")

Output:  Hello World!

Printing Using String Formatting:

Python provides multiple ways to format output.

       (a) Using f-strings

Example :    name = "Naitik"

                                  age = 15

                                  print(f"My name is {name} and I am {age} years old.")

             Output: My name is Naitik and I am 15 years old.

        (b) Using format() Method

           print("My name is {} and I am {} years old.". format(name, age))

           Output:  My name is Naitik and I am 15 years old

       (c) Using % Operator (Old Method)

       print("My name is %s and I am %d years old." % (name, age))

             Output:   My name is Naitik and I am 15 years old

Block & Indentation

In Python, blocks and indentation are fundamental to its structure and syntax. Unlike many other programming languages that use curly braces {} or other symbols to define code blocks

Blocks: A block is a section of code that belongs together, such as the body of a function, a loop, a conditional statement, or a class.

Example:

if condition:

    # This is a block

    statement1

    statement2

Indentation:

  • Indentation is the leading whitespace (spaces or tabs) in a line that defines the level of the block.
  • All lines in the same block must be indented at the same level.
  • By convention, Python uses 4 spaces per indentation level, though using tabs is also allowed. Mixing spaces and tabs, however, leads to errors.