-2
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.

Prune
  • 76,765
  • 14
  • 60
  • 81

2 Answers2

0

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.

Aviv Goldgeier
  • 799
  • 7
  • 23
0

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.

Chandella07
  • 2,089
  • 14
  • 22