2

How do i convert an f-string in python so that it can be printed by python version older than 3.6?

print(f"Player 1: {move1}  Player 2: {move2}")

When I try to run this in Python (3.5.3) it gives me a syntax error.

pault
  • 41,343
  • 15
  • 107
  • 149
Amanda
  • 31
  • 2
  • 1
    Try `print("Player 1: {move1} Player 2: {move2}".format(move1=move1, move2=move2))` – pault Jun 10 '19 at 21:05
  • 1
    Personally I don't think your question is a duplicate of the one linked. The answer is to use the [`format()`](https://docs.python.org/3/library/stdtypes.html#str.format) method of the built-in `str` class, which will work in both current and past versions (upto the point when _that_ feature was added anyway — which was in Python 2.6). – martineau Jun 10 '19 at 21:50

1 Answers1

4
print("Player 1: {move1} Player 2: {move2}".format(move1=move1, move2=move2))

will work in most recent versions of python. As will

print("Player 1: {}  Player 2: {}".format(move1, move2))

and if you want to use the old formatting syntax that'll work in python2 as well:

print("Player 1: %s  Player 2: %s" % (move1, move2))
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53