name = 'marcog'
number = 42
print '%s %d' % (name, number)
above program is executed in Python 2 but not in Python 3.
%s
and %d
are place holders that can replace the values in python 2. I need to know similar syntax in Python 3.
name = 'marcog'
number = 42
print '%s %d' % (name, number)
above program is executed in Python 2 but not in Python 3.
%s
and %d
are place holders that can replace the values in python 2. I need to know similar syntax in Python 3.
there are a few ways to format strings in Python 3.
These should all work:
print('{} {}'.format(name, number})
print('{name} {number}'.format(name=name, number=number))
print(f'{name} {number})'
(in Python >=3.6)
More info: https://pyformat.info/
EDIT: As everyone else is pointing out, the issue is with the print
statement. These other, newer, formatting options are generally recommended though.
for python 3 the syntax would be:
print('%s %d' % (name, number))
In python 3 print is the function rather than the statement, placeholders (%s or %d) will work same.