r/CodingHelp • u/[deleted] • Aug 18 '21
[Python] Does anyone know why this code does not work?
30
u/jedwardsol Professional Coder Aug 18 '21
In Python (and many other languages) arguments to functions are separated with commas, not spaces.
11
u/oderjunks Aug 18 '21
missing concat alert
print('hello ' + name)PS: the solution is NOT a comma, that would cause two spaces
1
Aug 18 '21
[removed] — view removed comment
4
13
3
u/thedogz11 Aug 18 '21
Concatenate the strings with the variables.
Example:
print("hello " + name)
That syntax should work just fine
2
3
1
u/skellious Intermediate Coder Aug 18 '21
just FYI, if you're using python 3.6 or above, you can use f-strings:
print(f"My name is {name} and my birthday is {birth}")1
0
u/6t94twenty Aug 18 '21
in print u either give a single formatted strings or different strings are parameter
0
u/knoam Aug 18 '21
That code works fine. Its purpose was to throw a Syntax Error, right?
1
Aug 19 '21
no. the purpose was to collect name, birth, and job values from input and return them in print.
0
-2
u/DeadMan1236 Aug 18 '21
print("hello {}".format(name)) Basically say If I want multiple things in a single string use the .format() print("hello my name is {} and I am here to learn {}".format(name, language) Instead of: print("hello my name is " + name + " and I am hear to learn " + language)
15
u/TheBlackOut2 Aug 18 '21
Don’t listen to this garbage. F-strings are king.
name = catsorio
print(f”Hello my name is {name}.”)
3
-4
0
Aug 18 '21
[deleted]
2
u/UrAccountGotHacked Aug 18 '21
What do concaténation?
2
u/SaylorMan1496 Aug 18 '21
Concatenation is appending two strings of characters together
1
u/UrAccountGotHacked Aug 19 '21
Then that is the same thing of putting them separated with a comma ?
2
Aug 19 '21
when you use concatenation you define variables like
x = "1"
y = "2"
then actually concat them like:
z = x + y
print(z)
and it'll print 12 since you defined 1 and 2. it combines whatever you defined.
2
1
u/SaylorMan1496 Aug 19 '21
In the context of this yes but, it can be useful for more than that like
Mystring = “hello” + “ world” Print(Mystring)
Would print hello world, concatenation is making a new string, separating them with a comma in the print function is sending them as separate arguments for the print function, and the function then deals with it
0
11
u/yesnomaybe102 Aug 18 '21
https://pytutorial.com/python-variable-in-string
https://www.delftstack.com/howto/python/python-print-string-and-variable/
Read up on print in python perhaps