Separating using the + sign when reading user data

0

Excuse me for the basic question. It's just that I do not understand when we have to use + in Input . Here is an example of a command below. Why does not my code work when I put str(i) between commas?

i = 1
textos = []
texto = input("Digite o texto " + str(i) +" (aperte enter para sair):")
    
asked by anonymous 13.11.2017 / 15:46

2 answers

0

Your code does not work with commas because, by commas, you are implicitly creating a tuple (a data structure). In your case, the input function receives one string per parameter, not a tuple. If in doubt, always use the + sign for concatenation, or a function called format . Here's an example:

mensagem = 'Digite o texto {} (aperte enter para sair):'.format(i)
texto = input(mensagem)

where {} will be replaced by the value passed by parameter in the format method (in this case the variable i ).

    
13.11.2017 / 15:50
0

You can use the formatting operator % , for example:

i = 1
textos = []
texto = input("Digite o texto %d (aperte enter para sair):" % (i) )

Or you can use the format() method , for example :

i = 1
textos = []
texto = input("Digite o texto {} (aperte enter para sair):".format(i))
    
13.11.2017 / 19:10