Interview Questions-21-10-24
Interview Questions-21-10-24
Q. Explain with Example sep, end, %d, %f, %s and format, f" "
ANS:
sep:
The sep parameter in Python's print() function specifies the separator between the
arguments passed to print(). By default, the separator is a space ' ', but you can
change it to any character or string by setting the sep argument.
Example:=>
print('The','Kiran','Academy')
#The Kiran Academy
print('The','Kiran','Academy',sep="*")
#The*Kiran*Academy
end:
In Python, the end parameter in the print() function specifies what should be
printed at the end of the output. By default, it is set to "\n", which means it
adds a newline after printing the output. You can change it to any string or
character you prefer.
Example=>
print('Hello','Pooja')
#Hello Pooja
print("Hello","Pooja",end="!!!!")
#Hello Pooja!!!!
%d:
In Python, %d is a format specifier used in strings to format integers. It is part
of the older string formatting method (also known as the % formatting style). %d
specifically formats the value as a decimal (integer).
Sum = English+Marathi+Hindi
%f:
In Python, %f is a format specifier used to format floating-point numbers. It is
part of the old-style string formatting (using the % operator). %f formats a number
as a floating-point value with six decimal places by default.
%s:
In Python, %s is a format specifier used to format strings. It is part of the old-
style string formatting (using the % operator), and it can convert any object to a
string.
format():
In Python, the format() method is used for string formatting. It allows you to
create a formatted string by inserting values into placeholders marked by curly
braces {}. It's a more modern and flexible alternative to the old % formatting
style.
name = "Pooja"
weight = 56
height = 5.3
print("my name is {}. My weight={} and Height={}".format(name,weight,height))
#my name is Pooja. My weight=56 and Height=5.3
f"":
In Python, f"" (also called f-strings or formatted string literals) is a concise
and powerful way to format strings. Introduced in Python 3.6, f-strings allow you
to embed expressions inside string literals by prefixing the string with f or F and
placing expressions inside curly braces {}.
x = 10
y = 5
print(f"{x} + {y} = {x + y}")
#10 + 5 = 15