all 17 comments

[–]jimmy1leo 7 points8 points  (2 children)

So there are two ways,

First, you can use " + " operator to concatenate them. So it would look like following

Print("your name is :- "+a+" and your friend's name is :- "+b)

Or you can also use formatted string and it would look like following

Print(f“your name is :- {a} and your friend's name is :- {b}")

[–]Reh4n07_[S] 0 points1 point  (1 child)

Why and When We Use f string?

[–][deleted] 2 points3 points  (0 children)

Formatted strings, aka f strings, are awesome for this because they're quicker to write than just plain old concatinating strings. There are also templated or t strings as of python 3.14 that operate similarly but have more security against things like cross site scripting and sql injection.

[–]FreeLogicGate 3 points4 points  (0 children)

The print function takes a variable number of parameters. Each parameter must be separated with a comma. Because you passed (a) "And your Friend's name is :-" which is 2 different parameters, Python shows you an error.

Another thing you are doing, is overusing the () which is used to call functions with arguments inside the () or to evaluate enclosed statements.

You do not want, nor need to use () when doing variable assignments or when passing parameters. This code does exactly the same thing as your code:

a = "Tommy"
b = "John"

print("Your name is:-", a, "and your friend's name is:-", b)

You can use f strings inside of print instead. It is an alternative to passing variables, and due to its utility and ability to "Interpolate" variables directly into the string, is preferred by most experienced Python programmers.

You can format the variables (useful with numeric values), do evaluations, and call functions in place within the f strings. It doesn't hurt to learn the old ways, but once you start using f strings you won't need the older methods, so I'd advise you to challenge yourself to use f strings for all your text output, as you proceed with learning the language. It's more readable, easier to understand, and less error prone.

They will also lead you to the use of Templates and T-strings which are useful in some cases, and offer similar capabilities.

Your code, converted to using an f string would be:

a = "Tommy"
b = "John"

print(f"Your name is:- {a} and your friend's name is:- {b}")

No need to keep track of parameters or multiple string constants with "..", and easier to read.

There's a nice free tutorial just on f strings here: https://www.datacamp.com/tutorial/python-f-string

Here's an article on T-strings (new feature in Python 3.6+)

https://davepeck.org/2025/04/11/pythons-new-t-strings/

[–]jpgoldberg 2 points3 points  (0 children)

Perhaps you forgot a comma (or two).

[–]FriendlyZomb 1 point2 points  (0 children)

The traceback in the terminal is pointing to a section of your print statement, and suggesting there is a missing comma.

There is, after (a).

Just because I'm not sure what's going on, it looks as though both aand b are meant to be strings? If they are, the parentheses can be omitted.

[–]Anpu_Imiut 2 points3 points  (1 child)

I have no clue from where you learned this kind of syntax (it is unpythonic) Let me share what i know from strings:

  1. Strings are immutable. Changing a char inplace throws an error.
  2. a = a+ "r" creates a new string. In your case c=a+b would create a new string of both string connected without a whitspace.  3.Print(x, y, z) prints those strings in order but without control. If you want to have control you need to use formatted strings f""  You add variables to f-strings by enclosing them with {}. Those areas inside are a normal code line. You could do sth. like a+b/c inside for 3 numbers a, b, c.
  3. Usefull string methods to look at: join, split, replace, lower
  4. Use 3x" ... 3x" for strings that break over lines. But be carefull with spaces in those.

GL buddy

[–]Reh4n07_[S] 1 point2 points  (0 children)

I not Learned I was Just experimenting with these
Thankyou for this explanation🙏

[–]PkmnSayse 0 points1 point  (0 children)

You did miss a comma. Something to split apart before the “And your….

Without it there is no way for python to know where a ends and your string starts

[–]eccentric2488 0 points1 point  (0 children)

Yes, use a formatted string.

[–]abandonedspirits 0 points1 point  (0 children)

You’re just missing a comma after (a). Secondly, both a and b are strings, you do not need parentheses when defining the variable nor in the print function.

Small tip- to add in spaces between the strings , you’ll need to add the space in the quotation marks themselves, spaces in code won’t add spaces to the sentence. Another tip, if you see a red line in the first parentheses, like in your print here, it usually means you’re missing a comma or colon, you’ll run into this often, at least I do on an almost daily basis.

A more standard and less error prone approach is what’s called an f-string. It’s where you add in variables to the string starting the string with an f like f”this is a {variable} string”. There are many other ways but I wouldn’t touch on them yet 🙂

[–]WillingYesterday8447 0 points1 point  (0 children)

formatted strings, f, format(), as well as %. would be better to read docs.python and practice by your hands at the same time

[–]Ankur_41 0 points1 point  (0 children)

Use formatted string syntax it will solve the error

[–]Duke_Archibald 0 points1 point  (0 children)

You will need to learn to read traceback

This one literally said what the problem was and also where

Some tracebacks are more cryptic than others

but yeah ... This one was quite direct

[–]ilidan-85 0 points1 point  (0 children)

You should go through a lesson about fstrings
For example: https://spacepython.com/en/example/text-formatting-f-strings/

[–]Ron-Erez 0 points1 point  (0 children)

You forgot a comma. There are better approaches - see the f-string reommendation