Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

410
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda