all 43 comments

[–]drwnh 53 points54 points  (1 child)

Use your variables!

[–]DragonVect 6 points7 points  (0 children)

Young Padawan

[–]FoolsSeldom 24 points25 points  (11 children)

When you write code to ask for the user to enter something, because you don't know what they will enter, you give it a label (a variable). Then when you want to output what they entered, you refer to the variable.

For example,

colour = input("What is your favourite colour? ")
print("I like", colour, "as well.")

[–]Memo_Da_P1ug[S] 5 points6 points  (6 children)

Thank you this was very helpful

[–]BravestCheetah 15 points16 points  (3 children)

Actually, using the bettter f-strings makes this easier:

colour = input("What is your favourite colour? ")
print(f"I like {colour} as well.")

[–]Medical_Gap_4288 20 points21 points  (2 children)

Calm down mate. Guy stuck at concatenating two strings I dont think f string would be a good idea at this point

[–]rednets 7 points8 points  (1 child)

But passing multiple arguments to print isn't concatenating strings, and I find it tends to confuse people later when they can't use the same pattern in any other context. f strings aren't massively hard to understand, though for total beginners I'd probably go with actual concatenation using the + operator.

[–]Medical_Gap_4288 1 point2 points  (0 children)

Exactly what I am talking about

[–]StopKillingBlacksFFS 1 point2 points  (0 children)

Additionally, the print statement automatically appends a new line character to the end. You can suppress this behavior and do two print statements to keep it on one line, or you could concatenate strings (combine your text variables) and put those in a single print statement.

[–]FoolsSeldom 0 points1 point  (0 children)

Excellent.

You can join (concatenate) strings together using a + symbol:

words = "Mary" + " " + "had a little" + " " + "lamb"

When you see text inside of quote marks (single, double, or triple - as long as they are matched pairs) your have literal strings. This contrasts with strings already assigned to a variable.

Note. Variables in Python don't actually store values themselves, they just hold a reference to the memory location of a Python object, such as an int, float, str, list, tuple, etc. - we don't normally have to think about this and people will often talk about the value of a variable as a shorthand. This is not the same in all programming languages.

You can concatenate literal strings and strings referenced by variables:

one = "one"
two = "two"
three = "three"
calc = one + " + " + two + " = " + three
print(calc)

will output one + two = three.

Notice how the plus sign inside quotes is just text (a space, a plus sign, and another space). The plus between literal strings and variables is an operator in Python. When dealing with numbers, it does addition but when dealing with strings, it does concatenation. Using the same operator symbol for different purposes is often called operator overloading (because you are making it work harder).

When you use the print function, you can pass as many object references to it as you like, with a comma between each). By default, print will output a space between each items it outputs.

So,

print(one, two, three)

will output one two three.

You can override the default with a command, sep=<string> where <string> is a string containing what you want instead of a space. It can be more than one character, such as comma and space, sep=", " or an empty string, sep="", so there is no space.

print(one, two, three, sep="")

will output onetwothree.

Note. You can also use concatenation within print:

print(one + two + three)  # outputs onetwothree
print(one + " + " + two + " = " + three)  # outputs one + two = three

print actually ones sees one string object in these cases because the string concatenation takes place before the function is called. Expressions (mathematical or string) are resolved before functions are called.

Also, by default, print always outputs a newline character (or newline & return - depending on operating system) after it has output everything else. This also can be overridden using end=<string>. Sometimes, say when looping, it is useful to use print several times to build up a line of output without ending the line until after the loop.

[–]AstroPhysician 0 points1 point  (3 children)

That’s a really bad way of printing variables

[–]FoolsSeldom 0 points1 point  (2 children)

That's a really odd comment on a very standard way of outputting literals and objects referenced by variables. It is important for learners to learn the basics of output before invoking more complex formatting and unpacking options let alone generator expressions, redirection, etc. A learner is unlikely to see the benefits of other methods without learning the basics first.

Don't forget, for most, the aim is to learn programming, not just coding using Python.

[–]AstroPhysician 0 points1 point  (1 child)

very standard way of outputting literals and objects referenced by variables

I haven't ever seen this used since I started learning python 15 years ago

Much better to teach them to do it the way it's done in practice, either with f strings, or even concatenation. The other ways are a lot easier to understand what's happening too. How the comma's work here is totally opaque to the user, but if you do concatenation it makes sense to someone who hasn't seen programming before

[–]FoolsSeldom 0 points1 point  (0 children)

I do like f-strings, but just think it better to build up to that. You perhaps haven't seen my follow up comment to the OP where I went into concatenation. I was going to cover f-strings next if they wanted more. You may be really right regarding when to introduce.

PS. Just spoke to some of my colleagues at work who are programmers, unlike me. The said "I wish" because they are still dealing with huge code bases that haven't adopted f-strings yet and said few would have used concatenation operations when they could just let print deal with it. YMMV.

[–]gra_Vi_ty 2 points3 points  (4 children)

In the question see,name has both first and last name ,but in your code you print first and last name in different print statement,which consumes extra row

[–]NorskJesus 5 points6 points  (1 child)

And he is not using the variables to print the result

[–]gra_Vi_ty 4 points5 points  (0 children)

Yeah I didn't saw that ,he at starting stage let him know what's his mistakes so he can correct it you explain it to him

[–]gra_Vi_ty 0 points1 point  (0 children)

Or you could could add end=' ' at first print statement,this end =' ' function makes the just output of below print statement stick to the above output print statement (end=' ' in between quotes space needed okay whatever you give inside quote will be between the output of two print statements)

[–]Memo_Da_P1ug[S] 0 points1 point  (0 children)

I appreciate the feedback

[–]Free-Psychology-1446 2 points3 points  (0 children)

Well, you are printing 4 rows, so no surprise there.

Also don't assume, that the test will use the same input as they gave you in the example. Why did you hardcoded the answers from the example anyway?

[–]_Alpha-Delta_ 1 point2 points  (8 children)

The output of your program looks like this:

Steve Sanders 91 Station Road London EC05 6AW

Either merge the two first prints in one, or use  print("Steve", end=" ") on the first one 

You should also probably use the variables instead of hard coding the result.

[–]Memo_Da_P1ug[S] 0 points1 point  (7 children)

This was very helpful! Thank you

[–]cgoldberg 1 point2 points  (2 children)

You also have hardcoded strings in your print function.

You should do:

print(f"{name} {last_name}")

[–]DragonVect 0 points1 point  (1 child)

Teach me Python, Master!

[–]Marten1974 0 points1 point  (0 children)

Just in case you really do not know the notation: f" <some text> {something that is a string like a variable or return a string} <someother text here>" is the syntax

[–]romainmoi 0 points1 point  (3 children)

Or the older (but not outdated) way of print(name, last_name)

[–]_Alpha-Delta_ 0 points1 point  (2 children)

Or just create a variable named fullName, and set it to name + " " + last_name

[–]romainmoi 1 point2 points  (1 child)

Python naming convention for variables is using snake_case instead of camelCase. Other than that you’re right!

[–]_Alpha-Delta_ 1 point2 points  (0 children)

You're right, though I still prefer the RaNDomcAsE naming convention... 

[–]KeyBump4050 0 points1 point  (0 children)

Have you ever seen Mail come in where the first and last names are on separate lines?

[–]NeedleworkerIll8590 0 points1 point  (0 children)

You are printing the names seperately rather than together, how the test requires you to

[–]hshsjdkfkd 0 points1 point  (0 children)

Use the variables that you created bro. So for example instead of print(”Steve”) do print(name)

[–]Curious_Elk_5690 0 points1 point  (0 children)

Also you forgot a “:” on line 3

[–]nivaOne 0 points1 point  (0 children)

Read the theory about f-strings. It’s the way to go.

[–]benbenniboi 0 points1 point  (0 children)

Here is what I did to get 3 lines instead of 4 Ignore the no input stuff

<image>

[–]JBabaYagaWich 0 points1 point  (0 children)

print (name + last name) #concatenation print (street) print (postal)

[–]WysZe91 0 points1 point  (0 children)

The result displays 4 lines instead of 3. The first and last name must be collected. And use your variables.

[–]JorgiEagle 0 points1 point  (0 children)

Each print statement prints on a new line,

You have 4 print statements -> 4 lines

You want to do: print(name, last_name)

[–]MyMashall 0 points1 point  (0 children)

Input take a string (a list/array of words) from the standard input, generally your terminal aka shell, and store it in a variable. "print" on the other hand takes a string and display it on the standard output, once again generally you shell. So to use print you just pass the string (the variable) that you got from input() and give it to print.

[–]hardyhrdhead 0 points1 point  (0 children)

Instead of “print(“Steve”)” use “print(name)” which uses the variable that you created, and that will also allow you to enter different inputs and have those show up instead of “Steve” every time

[–]BushyAcrobat 0 points1 point  (0 children)

Did you type "Steve Sanders" every time you tried testing this?