ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Python Print() Syntax

    IT Discussion
    python python print
    7
    29
    2.7k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • momurdaM
      momurda
      last edited by scottalanmiller

      Say i want to print some stuff to terminal screen

      name = "matt"
      dob = "1980"
      print(name,dob)
      print(name + " ",dob)
      

      Result is the same. Is there a reason Thonny chooses to teach me method 2? Is it just to illustrate concatenation with strings and variables in print function?

      ObsolesceO 1 Reply Last reply Reply Quote 1
      • scottalanmillerS
        scottalanmiller
        last edited by

        Probably for educational reasons, but might easily be so that it is Python 2 and Python 3 compatible.

        1 Reply Last reply Reply Quote 0
        • matteo nunziatiM
          matteo nunziati
          last edited by

          0_1520458514992_3ae13f5a-90bd-4d16-8d36-9b16c018001a-image.png
          that's not the same! please look at the added space after name:
          ('matt ','1980')
          (........^...........)

          1 Reply Last reply Reply Quote 0
          • scottalanmillerS
            scottalanmiller
            last edited by

            Same results here, not the same:

            >>> name = "Scott"
            >>> dob = 1976
            >>> print(name,dob)
            ('Scott', 1976)
            >>> print(name + " ",dob)
            ('Scott ', 1976)
            
            1 Reply Last reply Reply Quote 0
            • scottalanmillerS
              scottalanmiller
              last edited by

              I'm on Python 2.7 for that test.

              1 Reply Last reply Reply Quote 0
              • DustinB3403D
                DustinB3403
                last edited by

                I'm not a python programmer in anyway and could tell you the same thing would occur with excel, you're specifically adding in the empty space in the second line.

                It's different in every way.

                1 Reply Last reply Reply Quote 1
                • matteo nunziatiM
                  matteo nunziati
                  last edited by

                  tested both python 2.7 and 3.4, while the print() output is different, still the added space is there.

                  1 Reply Last reply Reply Quote 0
                  • momurdaM
                    momurda
                    last edited by

                    Wth i tested this like 30 times before posting the OP.
                    but now it is adding another space, i swear it wasnt.
                    How bout this one:

                    print(name +" ",dob)
                    print(name," ",dob)
                    

                    Why? From what i can tell these should be the same output but are not.

                    There are also differences in output between running python from terminal and thonny. I suppose this is just a formatting thing for the shell or for Thonny making things look nice.
                    0_1520459767867_4fbd9a5b-380b-4554-8544-d9343c760662-image.png
                    Now i see my python in cmd terminal is running 2.7 for some reason.

                    1 Reply Last reply Reply Quote 0
                    • scottalanmillerS
                      scottalanmiller
                      last edited by

                      So I've been programming for 33 years, so don't take this as anything but, when I test those three, they all give me exactly what I expect them to give me. So I think Python is doing the right thing, I'm going to guess that you have an odd expectation here. Since we don't know what that expectation is, it's hard to point to where your thinking is incorrect.

                      Here is what I get:

                      >>> name = "Scott"
                      >>> dob = 1976
                      >>> print(name,dob)
                      ('Scott', 1976)
                      >>> print(name + " ",dob)
                      ('Scott ', 1976)
                      >>> print(name," ",dob)
                      ('Scott', ' ', 1976)
                      
                      JaredBuschJ momurdaM 2 Replies Last reply Reply Quote 1
                      • scottalanmillerS
                        scottalanmiller
                        last edited by scottalanmiller

                        Now... why?

                        >>> print(name,dob)
                        ('Scott', 1976)
                        
                        

                        Print: name comma dob

                        That's exactly what it prints. There are no spaces specified anywhere. You specify the contents of the name variable, a comma, and the contents of the dob variable.

                        1 Reply Last reply Reply Quote 0
                        • JaredBuschJ
                          JaredBusch @scottalanmiller
                          last edited by

                          @scottalanmiller said in Python Print() Syntax:

                          So I think Python is doing the right thing, I'm going to guess that you have an odd expectation here.

                          Yes, Python is doing exactly what it should.

                          What are you thinking you should see @momurda?

                          1 Reply Last reply Reply Quote 0
                          • momurdaM
                            momurda @scottalanmiller
                            last edited by

                            @scottalanmiller I dont have any expectation.
                            yours i think is output from 2.7
                            3.6 looks like
                            0_1520460234077_335fdbb5-30bb-4360-8591-2bba4fe5fe5f-image.png
                            as well as my Thonny install which uses 3.6

                            scottalanmillerS 1 Reply Last reply Reply Quote 0
                            • scottalanmillerS
                              scottalanmiller
                              last edited by

                              And part two:

                              >>> print(name + " ",dob)
                              ('Scott ', 1976)
                              

                              Print: name concatenated with a space comma dob

                              What they are trying to teach you here is that the concatenation operator "+" takes precedence over other formatting so is tacking the space you specify onto the end of the "name" variable before it formats the print statement. So when it prints out the comma is still against the quote, but there is a space inside of the quotes. That's because the resulting string is inside the quotes, and the space is part of that string.

                              1 Reply Last reply Reply Quote 1
                              • scottalanmillerS
                                scottalanmiller @momurda
                                last edited by

                                @momurda said in Python Print() Syntax:

                                @scottalanmiller I dont have any expectation.
                                yours i think is output from 2.7
                                3.6 looks like
                                0_1520460234077_335fdbb5-30bb-4360-8591-2bba4fe5fe5f-image.png
                                as well as my Thonny install which uses 3.6

                                Python 3 changed a lot, enough that it is considered a new language.

                                1 Reply Last reply Reply Quote 0
                                • scottalanmillerS
                                  scottalanmiller
                                  last edited by

                                  So in Python 3:

                                  >>> name = "scott"
                                  >>> dob = 1976
                                  >>> print(name,dob)
                                  scott 1976
                                  >>> print(name + " ",dob)
                                  scott  1976
                                  >>> print(name," ",dob)
                                  scott   1976
                                  

                                  The big change to Python 3 is that the comma is no longer treated as a character, but is now processed as a separator.

                                  momurdaM 1 Reply Last reply Reply Quote 2
                                  • momurdaM
                                    momurda @scottalanmiller
                                    last edited by

                                    @scottalanmiller said in python syntax:

                                    So in Python 3:

                                    >>> name = "scott"
                                    >>> dob = 1976
                                    >>> print(name,dob)
                                    scott 1976
                                    >>> print(name + " ",dob)
                                    scott  1976
                                    >>> print(name," ",dob)
                                    scott   1976
                                    

                                    The big change to Python 3 is that the comma is no longer treated as a character, but is now processed as a separator.

                                    This bit in particular incredibly important i think. The , as separator is most common in other languages yes? I think this is the root of my confusion here.

                                    I suppose i could have read the changelog from 2.7 to 3.6 but i probably wouldnt understand most of it at this point.

                                    scottalanmillerS 1 Reply Last reply Reply Quote 0
                                    • scottalanmillerS
                                      scottalanmiller @momurda
                                      last edited by

                                      @momurda said in Python Print() Syntax:

                                      @scottalanmiller said in python syntax:

                                      So in Python 3:

                                      >>> name = "scott"
                                      >>> dob = 1976
                                      >>> print(name,dob)
                                      scott 1976
                                      >>> print(name + " ",dob)
                                      scott  1976
                                      >>> print(name," ",dob)
                                      scott   1976
                                      

                                      The big change to Python 3 is that the comma is no longer treated as a character, but is now processed as a separator.

                                      This bit in particular incredibly important i think. The , as separator is most common in other languages yes? I think this is the root of my confusion here.

                                      I suppose i could have read the changelog from 2.7 to 3.6 but i probably wouldnt understand most of it at this point.

                                      Not uncommon, but something you have to know how it is treated in each language. Python is white space gnostic, which is extremely rare and confusing to people from most languages.

                                      1 Reply Last reply Reply Quote 1
                                      • scottalanmillerS
                                        scottalanmiller
                                        last edited by

                                        Here is Ruby doing something similar:

                                        irb(main):001:0> name = "Scott"
                                        => "Scott"
                                        irb(main):002:0> dob = 1976
                                        => 1976
                                        irb(main):005:0> print "#{name},#{dob}"
                                        Scott,1976=> nil
                                        irb(main):006:0> print "#{name} #{dob}"
                                        Scott 1976=> nil
                                        irb(main):007:0> print "#{name} " " #{dob}"
                                        Scott  1976=> nil
                                        irb(main):008:0> print "#{name}, " ", #{dob}"
                                        Scott, , 1976=> nil
                                        

                                        Ruby prints the comma, it does not use it as a separator.

                                        1 Reply Last reply Reply Quote 1
                                        • RomoR
                                          Romo
                                          last edited by

                                          Python's 3 print() is a function . Your variables are the parameters for the print function.

                                          print(parameter1,parameter2)
                                          

                                          Python's 2 print is a special statement not a function so when when you put your variable names inside the print statement python prints out your variables as a tuple because of the , .

                                          >>>print (name)
                                          romo
                                          
                                          >>> print(name,)
                                          ('romo',)
                                          
                                          
                                          1 Reply Last reply Reply Quote 3
                                          • matteo nunziatiM
                                            matteo nunziati
                                            last edited by

                                            to be clear:
                                            in python 2.x what you get with a print() is a tuple of elements. the comma is there NOT because it is a separator but because it is used in print() to concatenate multiple items (Python objects) in a tuple.

                                            in python 3.x the print() statement print EVERYTHING if it is a string and concatenate the objects into a single string. If you do not pass a string but an object has a __str__() method, it is implicitly invoked.

                                            trivial example in python 2, create a tuple:

                                            t=("rob",21)
                                            print(t)
                                            

                                            try to print it. you get exactly the same result than your example. then try the same in python 3.x. again...

                                            the comma in the print statement is misleading you. it is just command synthax nothing to do with a char in a string or anything similar. when you see stuff into rounded brackets, this is a tuple. and by default elements in a tuple are separated by a comma.

                                            (to be accurate the representation of a tuple according to its default __str__() method)

                                            NOW
                                            try the same thing calling tuple items explicitly:

                                            print(t[0], t[1])
                                            

                                            output is different in python3! In this sense pythion 3 is more accurate in the data representation.

                                            EVENTUALLY
                                            if you really want a comma separated list of items into a string you must format it:

                                             print("%s; %s" % t)
                                            

                                            in this case I've used a semicolon to point out the difference.

                                            1 Reply Last reply Reply Quote 2
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post