How to eliminate white space after backslash (Python 3.4) -
looking advice text formatting. trying print results of simple unit conversion (imperial metric) keep getting these spaces when trying print double apostrophe symbol.
code:
print("you entered ", imp_height_flt, "\" converts " \ "%.2f" % imp_height_converted, "m.") when run program get:
you entered 5.9 " converts 1.80 m. i'm trying eliminate space between 9 , double apostrophe.
use formatting whole string:
print("you entered %.1f\" converts " "%.2fm." % (imp_height_flt, imp_height_converted)) or tell print() function not use spaces separator:
print("you entered ", imp_height_flt, "\" converts " "%.2f" % imp_height_converted, "m.", sep='') the default sep argument ' ', space, can set empty string.
note backslash @ end of first line not needed @ since (..) parentheses form logical line already.
personally, i'd use str.format() method string template here; more flexible , powerful method interpolating values string:
print('you entered {:.1f}" converts ' '{:.2f}m.'.format(imp_height_flt, imp_height_converted)) i used single quotes form string literal, embedded " doesn't have use backslash either.
demo:
>>> imp_height_flt, imp_height_converted = 5.9, 1.8 >>> print("you entered %.1f\" converts " ... "%.2fm." % (imp_height_flt, imp_height_converted)) entered 5.9" converts 1.80m. >>> print("you entered ", imp_height_flt, "\" converts " ... "%.2f" % imp_height_converted, "m.", ... sep='') entered 5.9" converts 1.80m. >>> print('you entered {:.1f}" converts ' ... '{:.2f}m.'.format(imp_height_flt, imp_height_converted)) entered 5.9" converts 1.80m.
Comments
Post a Comment