How does python know to add a space between a string literal and a variable? -


so learning python hard way. in 1 of exercises print string literals variables between them. noticed unlike other languages python automatically adds space between string literal , variables. curious how that. below example of mean.

example.py:

total_so_questions_asked = 1 print "i have asked", total_so_questions_asked, "question far."  

terminal:

$ python example.py have asked 1 question far. 

this silly , not important curious, , hoping can enlighten me!

it's cool feature of python.

there 2 things having multiple arguments does: adds space around parameters necassary, , converts each of arguments string seperately.

let's @ how simple can make potentially complicated code using these features:

a = 5 b = 3 c = + b print a, "plus", b, "equals", a+b 

if couldn't have list of individually casted parameters, it'd ugly:

print str(a) + " plus " + str(b) + " equals " + str(a+b) 

from zen of python, lines 1 , 3:

beautiful better ugly.

simple better complex.

check out python reference more info.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -