Does Python syntax accept either "+" or "," in the "print()" command?

Asked

Viewed 105 times

5

For example, if I wanted to print a string that I create on time along with a ready-made variable, any of the cases would work?


#Case 1

nome = "Júlia"

print("Olá " + nome)

#Case 2

nome = "Júlia"

print("Olá ",nome)

2 answers

7

"+" and "," in the print, are two very different things "under the hood" in Python - although the result may be similar or even the same.

It’s important to understand what happens - and why "goes both ways," and even understand even better ways.

First: the print in Python 3 is not a "command" (it was in Python 2). It is a function - equal to any function you define in your program.

And, functions in Python, may have to receive an exact number of parameters, or they may receive a variable number of parameters - that’s part of the language.

In the case, the print is a function that takes a variable number of parameters in sequence. What it does is: convert each parameter to a string (inside it calls str(parametro) ), and write in the special file sys.stdout these parameters, interspersed by space (the string " "). Both this separator and the output print file can be modified by parameters with name ("Sep" and "file") - later I talk about this.

The operator "+" concatenates strings! That is, if you do "nome: " + variavel this will create a single string, result of the expression - and this string is passed as a single parameter to "print". The most important thing here is that the "+" works both within the parentheses of the print, as at any point in the program - it is simply a Python expression, as well as the expression 2 + 2 - which is transformed into "4" when it is executed.

In practice, if the two operands are strings, almost no matter when calling "print" (but it has the difference that, by default, the separation of the parameters with , introduces the separation space).

Examples:

>>> a = "Júlia"
>>> print ("nome", a)
nome Júlia
>>> print("nome" + a)
nomeJúlia
>>> print ("nome", a, sep="****")
nome****Júlia
>>> print("nome" + a, sep="****")
nomeJúlia
>>> b = "nome" + a
>>> print(b)
nomeJúlia
>>> print ("idade:", 23)
idade: 23
>>> print ("idade:" +  23)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int

Another way to interpolate string values

From Python 3.6, however, a new syntax has been created that makes it much more practical to place variable values (and other Python expressions) within texts - they are "f-strings". The same thing was possible before, but with the call of the method format of strings, which in addition to forcing you to write more, forces you to write out of order - you put all the text, and then, in the parameters of .format will think about which variables will use:

Ancient method:

nome = "júlia"
idade = 23

print("Olá, {}. Vejo que você tem {} anos.".format(nome, idade)

New method with f-strings:

nome = "júlia"
idade = 23

print(f"Olá, {nome}. Vejo que você tem {idade} anos.")

(Note the f before the ")

Just like in the case of "+", this is part of the syntax of the language, and will work both in a call to print, as in any other point - expression or function call in your code. (The rule is: if the string has the prefix f for quotation marks, such as f"...", within it, any text that comes within { } is executed as Python code, the result is converted to string, and embedded in the external string.)

This form is interesting because it avoids having to keep closing and opening " all the time, and the code becomes much more readable and easy to type.

3

These two ways of printing a string work perfectly and have no differences in the final result, but the process is different.

In the first case, you are concatenating the string "Olá " with the string nome before calling the function. Therefore, this is equivalent to making the following code:

print("Olá Júlia")

In the second case, you are passing the strings separately as argument to the function print(), which in turn will join the strings with a space between them and then print them on the screen.

  • It’s really...so simple,kkkkk I know the site doesn’t like comments like that,but thank you so much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.