Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
Getting an "invalid syntax" when trying to perform string interpolation

I have recently been learning python 3 and I cannot get any examples involving string interpolation (formatting) to work.

In [1]: state = "Washington"

In [2]: state
Out[2]: 'Washington'

In [3]: my_message = f"I live in {state}"
File "<ipython-input-3-d004dd9e0255>", line 1
my_message = f"I live in {state}"
                                ^
SyntaxError: invalid syntax

I figured my machine was defaulting to python 2, but a quick check reveals:

Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
Type "copyright", "credits" or "license" for more information.

IPython 5.2.2 -- An enhanced Interactive Python.

I am on Ubuntu 16.04:

python3 --version
Python 3.5.2

Am I just overlooking basic syntax? I have run the same commands on a few computers from fellow students and it seems to execute just fine.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

As suggested by Josh Lee in the comment section, that kind of string interpolation was added in Python 3.6 only, see What’s New In Python 3.6 (here it's called "PEP 498: Formatted string literals").

You, however, seems to be using Python 3.5.2, which does not support that syntax.

over 4 years ago · Santiago Trujillo Denunciar

0

This is a pretty old question and not sure if answered somewhere else, but ran into same problem and landed on some confusing pages. Figured out a couple of minutes later. Below line should work.

my_message = "I live in {}".format(state)

.format works for 3.5. Documenting it here for someone who may need it for similar issue.

over 4 years ago · Santiago Trujillo Denunciar

0

What worked for me (in python 3.8.5 in sublime) was removing the f.

message = "I live in {state}"

I find it easier than .format(state)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda