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.
    • RomoR
      Romo
      last edited by

      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.

      Just to add a little bit to the formatting of strings.

      If you are using python 3.6+ you can use formatted string literals or f-strings which in my opinion are much easier to read and use, rather than formatting with % or .format() or concatenating strings.

      print(f'{name},{dob}')
      romo,1986
      
      1 Reply Last reply Reply Quote 1
      • ObsolesceO
        Obsolesce @momurda
        last edited by

        @momurda said in Python Print() Syntax:

        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?

        For comparison, this is how I'd do that same code in PHP:

        <?php
            $name = 'tim';
            $year = '2018';
            echo $name . " " . $year;
        ?>
        
        scottalanmillerS 1 Reply Last reply Reply Quote 0
        • scottalanmillerS
          scottalanmiller @Obsolesce
          last edited by

          @tim_g said in Python Print() Syntax:

          @momurda said in Python Print() Syntax:

          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?

          For comparison, this is how I'd do that same code in PHP:

          <?php
              $name = 'tim';
              $year = '2018';
              echo $name . " " . $year;
          ?>
          

          Well, that's how you'd do it in PHP that is encapsulated inside of HTML. Not really how PHP does it exactly.

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

            PHP itself looks like this in a REPL...

               $name = "Scott";
            => "Scott"   $dob = "1976";
            => "1976"   print $name . $dob;
            Scott1976  
            
            1 Reply Last reply Reply Quote 0
            • ObsolesceO
              Obsolesce @scottalanmiller
              last edited by

              @scottalanmiller said in Python Print() Syntax:

              @tim_g said in Python Print() Syntax:

              @momurda said in Python Print() Syntax:

              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?

              For comparison, this is how I'd do that same code in PHP:

              <?php
                  $name = 'tim';
                  $year = '2018';
                  echo $name . " " . $year;
              ?>
              

              Well, that's how you'd do it in PHP that is encapsulated inside of HTML. Not really how PHP does it exactly.

              What's it matter... anything inside of the PHP tags is being parsed by PHP.
              That's how you do it also in a PHP file with no html. I'm not sure what your point is.

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

                @tim_g said in Python Print() Syntax:

                @scottalanmiller said in Python Print() Syntax:

                @tim_g said in Python Print() Syntax:

                @momurda said in Python Print() Syntax:

                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?

                For comparison, this is how I'd do that same code in PHP:

                <?php
                    $name = 'tim';
                    $year = '2018';
                    echo $name . " " . $year;
                ?>
                

                Well, that's how you'd do it in PHP that is encapsulated inside of HTML. Not really how PHP does it exactly.

                What's it matter... anything inside of the PHP tags is being parsed by PHP.
                That's how you do it also in a PHP file with no html. I'm not sure what your point is.

                PHP is often taught as only existing inside HTML, rather than being its own language. It can be very confusing. The average person using PHP doesn't even realize that you can run PHP scripts.

                ObsolesceO 1 Reply Last reply Reply Quote 0
                • ObsolesceO
                  Obsolesce @scottalanmiller
                  last edited by

                  @scottalanmiller said in Python Print() Syntax:

                  @tim_g said in Python Print() Syntax:

                  @scottalanmiller said in Python Print() Syntax:

                  @tim_g said in Python Print() Syntax:

                  @momurda said in Python Print() Syntax:

                  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?

                  For comparison, this is how I'd do that same code in PHP:

                  <?php
                      $name = 'tim';
                      $year = '2018';
                      echo $name . " " . $year;
                  ?>
                  

                  Well, that's how you'd do it in PHP that is encapsulated inside of HTML. Not really how PHP does it exactly.

                  What's it matter... anything inside of the PHP tags is being parsed by PHP.
                  That's how you do it also in a PHP file with no html. I'm not sure what your point is.

                  PHP is often taught as only existing inside HTML, rather than being its own language. It can be very confusing. The average person using PHP doesn't even realize that you can run PHP scripts.

                  I've been using PHP to output html. Where PHP is understanding html too, rather than the other way.

                  ObsolesceO 1 Reply Last reply Reply Quote 0
                  • ObsolesceO
                    Obsolesce @Obsolesce
                    last edited by

                    @tim_g said in Python Print() Syntax:

                    @scottalanmiller said in Python Print() Syntax:

                    @tim_g said in Python Print() Syntax:

                    @scottalanmiller said in Python Print() Syntax:

                    @tim_g said in Python Print() Syntax:

                    @momurda said in Python Print() Syntax:

                    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?

                    For comparison, this is how I'd do that same code in PHP:

                    <?php
                        $name = 'tim';
                        $year = '2018';
                        echo $name . " " . $year;
                    ?>
                    

                    Well, that's how you'd do it in PHP that is encapsulated inside of HTML. Not really how PHP does it exactly.

                    What's it matter... anything inside of the PHP tags is being parsed by PHP.
                    That's how you do it also in a PHP file with no html. I'm not sure what your point is.

                    PHP is often taught as only existing inside HTML, rather than being its own language. It can be very confusing. The average person using PHP doesn't even realize that you can run PHP scripts.

                    I've been using PHP to output html. Where PHP is understanding html too, rather than the other way.

                    It can do anything really. Most PHP is that way I seen, rather than being inside of an html file.

                    Unless I'm doing it wrong...

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

                      @tim_g said in Python Print() Syntax:

                      @tim_g said in Python Print() Syntax:

                      @scottalanmiller said in Python Print() Syntax:

                      @tim_g said in Python Print() Syntax:

                      @scottalanmiller said in Python Print() Syntax:

                      @tim_g said in Python Print() Syntax:

                      @momurda said in Python Print() Syntax:

                      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?

                      For comparison, this is how I'd do that same code in PHP:

                      <?php
                          $name = 'tim';
                          $year = '2018';
                          echo $name . " " . $year;
                      ?>
                      

                      Well, that's how you'd do it in PHP that is encapsulated inside of HTML. Not really how PHP does it exactly.

                      What's it matter... anything inside of the PHP tags is being parsed by PHP.
                      That's how you do it also in a PHP file with no html. I'm not sure what your point is.

                      PHP is often taught as only existing inside HTML, rather than being its own language. It can be very confusing. The average person using PHP doesn't even realize that you can run PHP scripts.

                      I've been using PHP to output html. Where PHP is understanding html too, rather than the other way.

                      It can do anything really. Most PHP is that way I seen, rather than being inside of an html file.

                      Unless I'm doing it wrong...

                      Nearly all PHP is written to be called by a web server. PHP can do anything, but it's so commonly used as a file on a web server that it is almost exclusively assumed to be what it is being used for.

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