+ operator

A forum for general discussion of the Python programming language.

+ operator

Postby ozcee » Wed Jan 15, 2014 7:02 am

Just curious here, I am working my way through the book "Learn Python the hard way". In exercise 5 it uses the expression
Code: Select all
print w + e
to display the two strings. What does the "+" do? Would I not get the same result with
Code: Select all
print w, e
?

regards
Cliff
Last edited by Mekire on Wed Jan 15, 2014 7:16 am, edited 1 time in total.
Reason: First post lock.
ozcee
 
Posts: 2
Joined: Wed Jan 15, 2014 6:54 am

Re: + operator

Postby Mekire » Wed Jan 15, 2014 7:26 am

Well firstly, they don't give the exact same thing:
Code: Select all
>>> a = "some"
>>> b = "string"
>>> print a+b
somestring
>>> print a,b
some string
As you can see the second version adds a space where the comma is.

The + in the first version concatenates the strings together. The second version is just how the print statement works pre-python 3.

Make sure that you understand that these two things aren't equal as expressions:
Code: Select all
>>> a = "some"
>>> b = "string"
>>> c = a+b
>>> c
'somestring'
>>> d = a,b
>>> d
('some', 'string')
>>>
In a normal context separating variables with a comma creates a tuple. The print statement however has its own syntax.

Honestly even if you are using python 2 I would avoid printing like that. It is however OK when starting out.

As you advance you will be much more likely to use string formatting than separating your arguments by commas.

-Mek
User avatar
Mekire
 
Posts: 830
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: + operator

Postby ozcee » Wed Jan 15, 2014 8:45 am

Mek Thanks for clearing that up. As I progress I will no doubt ask further questions
ozcee
 
Posts: 2
Joined: Wed Jan 15, 2014 6:54 am

Re: + operator

Postby Marbelous » Wed Jan 15, 2014 3:23 pm

ozcee wrote:Mek Thanks for clearing that up. As I progress I will no doubt ask further questions

That's good but don't forget all these basic questions are answered in the first few chapters of the Python Tutorial that is installed along with Python. (or any good python tut)
Take a look through the references too. It's a good idea to leave them open while your coding. Not only will they have the info you need without waiting on an internet forum, they provide new ideas where you can learn the advanced features of python faster. ;)
Marbelous
 
Posts: 68
Joined: Fri May 31, 2013 8:12 pm

Re: + operator

Postby metulburr » Wed Jan 15, 2014 6:06 pm

without waiting on an internet forum

You can join the IRC chat for sometimes quicker responses. See global stickies.
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1130
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest