Input() is a pre-defined function that reads any type of data from the Keyboard.
This function is used for reading any type of data from the Keyboard in the form of str.
Programmatically, we can convert str-type values into other types of values by using Type-casting Techniques.
# Prompt the user to enter input and store it in the variable 'var'
var = input()
# Display the value entered by the user and its data type
print(var, type(var))
# Prompt the user to enter a number and convert the input to an integer
var = int(input())
# Display the entered number and its data type
print(var, type(var))
# Prompt the user to enter any value and convert the input to an bool
var = bool(input())
# Display the entered value and its data type
print(var, type(var))
This function is used for reading any type of data from Keybord in the form of str by giving user-prompting message.
# Prompt the user to enter their age and convert the input to an integer
age = int(input("Enter your age: "))
# Display the entered age and its data type
print(age, type(age))
# Prompt the user to enter their name and store the input in the variable 'name'
name = input("Enter your name: ")
# Display the entered name and its data type
print(name, type(name))
The split() method breaks a string into a list of substrings based on a delimiter. By default, it splits on whitespace.
The split() method breaks a string into a list of substrings based on a delimiter. By default, it splits on whitespace.
split() ensures input is stored as a list.
# Taking input from the user
user_input = input("Enter words separated by spaces: ")
# Splitting the input into a list
words = user_input.split()
print("List of words:", words)
# Prompt the user to input two values separated by spaces
var1, var2 = input("Enter the values: ").split()
# 'input()' takes input from the user as a single string
# 'split()' breaks the input string into parts based on whitespace (default delimiter)
# The split values are unpacked into 'var1' and 'var2'
# Print the values stored in 'var1' and 'var2'
print(var1, var2)
This syntax display the Message along with values on the console.
# add two number
num1 = 10
num2 = 30
sum = num1+num2
print("The sum of two numbers are = ",sum)
# adding two numbers based on user input
num1 = input("enter the number1 = ")
num2 = input("enter the number2 = ")
num1 = int(num1)
num2 = int(num2)
num3 = num1+num2
print("The sum of " num1, " + ", num2, "is = ",num3)
# multification of two numbers based on user input
num1 = int(input("enter the number1 = "))
num2 = int(input("enter the number2 = "))
num3 = num1*num2
print("The product of " num1, " * ", num2, "is = ",num3)
In Python, formatted string literals, commonly known as f-strings, provide a concise and efficient way to embed expressions within string literals.
f-strings are prefixed with an f or F and use curly braces {} to evaluate expressions at runtime.
# To display the output using fstring
name = "Python"
print(f"Hello {name}!")
# add two numbers
num1 = 5
num2 = 10
print(f"The sum of {a} and {b} is {a + b}.")
# Assign values to variables 'a' and 'b'
a = 10
b = 20
# Calculate the product of 'a' and 'b' and store it in variable 'c'
c = a * b
# Print the multiplication result using the str.format() method
print("mul({}, {}) = {}".format(a, b, c))
# Print the multiplication result again using the str.format() method
# Note: The placeholder '{}' is used to insert the values of 'a', 'b', and 'a * b'
print("mul({}, {}) = {}".format(a, b, a * b))
In Python, the % operator is used for string formatting, allowing the insertion of values into a string with specified formats. Here's a concise overview:
Format specifier()
%d inserts integers.
%f inserts floating-point numbers.
%s Inserts strings or any object convertible to a string.
a = 10
b = 5
c = a + b
print("sum=%d" % c) # Prints the sum as an integer
print("sum=%f" % c) # Prints the sum as a floating-point number
print("sum=%0.2f" % c) # Prints the sum as a floating-point number with 2 decimal places
print("%d is the sum" % c) # Prints the sum in a sentence
stno = 10
name = "Rossum"
print("My Number is %d and name is %s" % (stno, name))
If you don't have any format specifier then those values converted into str and use %s
The purpose of format () is that it supplies the specified variables to the empty curly braces{}. themethod can format strings in Python by passing variables in a specified order. The variables are substituted into placeholders {} in the string. The order in which variables are passed to the .format() method determines their placement in the output.
# add two numbers
a = 10
b = 20
c = a+b
print("sum of {} and {}={}".format(a,b,c))
a = int(input())
b =int(input())
c = int(input())
d = a + b + c
print("Sum of {}, {} and {} = {}".format(a, b, c, d))
stno = 10
sname = "Rossum"
print("My Number is {} and name is {}".format(stno, sname))