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.

2
  • 1
    Try print("Player 1: {move1} Player 2: {move2}".format(move1=move1, move2=move2)) Commented Jun 10, 2019 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() 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). Commented Jun 10, 2019 at 21:50

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

3 Comments

Could also just do print(f"Player 1: {move1} Player 2: {move2}").
@Error-SyntacticalRemorse The question is how to replace the f-string with something compatible with earlier versions of Python.
@chepner I understand but the answer said: "will work in most recent versions of python" so I figured might as well put that there to since format is already there (which is for recent python versions).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.