python - Bracket Injection when Concatenating String -
i have python 3 program runs extremely 1 part. calculating area , perimeter of rectangle (drawn graphics.py
) , concatenating results 1 string. below current code
val = "the perimeter is" , str(2*(height + width)), " , area ", str(height*width)
when output page using graphics.py
, result shows following.
{the perimeter is} 215 { , area } 616
how can output not have brackets in middle of text? without str()
, brackets exist, not ideal result. ideal result below.
the perimeter 215 , area 616
if use +
instead of ,
, error can't convert 'int' object str implicitly
, hasn't worked me either.
any great!
when use ,
in variable definition, creating tuple not string. hence when -
val = "the perimeter is" , str(2*(height + width)), " , area ", str(height*width)
val
tuple
, reason brackets. suggest using str.format
create string. example -
val = "the perimeter {} , area {}".format(2*(height + width),height*width)
Comments
Post a Comment