Python Print() Syntax
-
@scottalanmiller I dont have any expectation.
yours i think is output from 2.7
3.6 looks like
as well as my Thonny install which uses 3.6 -
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.
-
@momurda said in Python Print() Syntax:
@scottalanmiller I dont have any expectation.
yours i think is output from 2.7
3.6 looks like
as well as my Thonny install which uses 3.6Python 3 changed a lot, enough that it is considered a new language.
-
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.
-
@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.
-
@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.
-
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.
-
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',)
-
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.
-
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
-
@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; ?>
-
@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.
-
PHP itself looks like this in a REPL...
$name = "Scott"; => "Scott" $dob = "1976"; => "1976" print $name . $dob; Scott1976
-
@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. -
@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.
-
@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.
-
@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...
-
@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.