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

407
Vistas
Django: How to convert string and int into variable

I have declared a variable as"

a1 = 10

Now in a function I need to call this variable by adding "a"+"1". However result is a1 in string and not above declared variable.

Here is the code:

a1 = 10
b = "a"+"1"
print(b)
a1

when I print b, answer is a1, instead of 10. How can I change this concatenate to declared variable?

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

0

if I understand, you want to dynamically create the name of a variable and access it : you can use dict for that i think

a1, a2, a3 = 10, 11, 12
var_dict = {'a1': a1, 'a2': a2, 'a3': a3}

for i in [1, 2, 3]:
    print(var_dict[f"a{i}"])  # f"a{i}" => 'a1', 'a2', 'a3' according to i value
over 4 years ago · Santiago Trujillo Denunciar

0

You can use python inbuilt function eval for such requirements like below :-

>> a1 = 10
>> b = "a"+"1"
>> print(eval(b))
>> 10
over 4 years ago · Santiago Trujillo Denunciar

0

The statement b = 'a'+'1' means that your new variable b is a string:

>>> b = ['a1']

If you are interested in printing a1, you have just to do:

print(a1)

Making a sum of the letters of a declared variable does not result in the variable itself.

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