29
loading...
This website collects cookies to deliver better user experience
%
) on a string, it is used for formatting, and if you are using it on an integer, it is for calculating the modulo.%
) on string variable. Since it cannot perform a division of string and get the reminder, Python will throw not all arguments converted during string formatting error.# Check even or odd scenario
number= (input("Enter a Number: "))
if(number % 2):
print("Given number is odd")
else:
print("Given number is even")
# Output
Enter a Number: 5
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 3, in <module>
if(number % 2):
TypeError: not all arguments converted during string formatting
# Check even or odd scenario
number= (input("Enter a Number: "))
if(int(number) % 2):
print("Given number is odd")
else:
print("Given number is even")
# Output
Enter a Number: 5
Given number is odd
name ="Jack"
age =20
country="India"
print("Student %s is %s years old "%(name,age,country))
# Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 5, in <module>
print("Student %s is %s years old "%(name,age,country))
TypeError: not all arguments converted during string formatting
name ="Jack"
age =20
country="India"
print("Student %s is %s years old and he is from %s "%(name,age,country))
# Output
Student Jack is 20 years old and he is from India
{}
and %
operators to perform string interpolation, so Python will throw TypeError in this case.# Print Name and age of Student
name = input("Enter name : ")
age = input("Enter Age : ")
# Print Name and Age of user
print("Student name is '{0}'and Age is '{1}'"% name, age)
# Output
Enter name : Chandler
Enter Age : 22
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 6, in <module>
print("Student name is '{0}'and Age is '{1}'"% name, age)
TypeError: not all arguments converted during string formatting
%
operator will be soon deprecated, instead use the modern approach {}
with .format()
method as shown below..format()
method replaces the values of {}
with the values specified in .format()
in the same order mentioned.# Print Name and age of Student
name = input("Enter name : ")
age = input("Enter Age : ")
# Print Name and Age of user
print("Student name is '{0}'and Age is '{1}'".format(name, age))
# Output
Enter name : Chandler
Enter Age : 22
Student name is 'Chandler'and Age is '22'