Regarding the behavior of string padding using str.ljust in python -


i have use case in setting labels

matplotlib.colorbar 

and want distribute description evenly using code below

temp = "" section in range(total_section_len):     temp.ljust(60//(total_section_len * 5))     temp += "stream 0"     temp.ljust(60//(total_section_len * 5))     temp += "stream 1"     temp.ljust(60//(total_section_len * 5)) print temp 

i expecting

"    stream0     stream1     " 

but instead

"stream0stream1" 

why

str.ljust  

behavior in such fashion?

thanks

the parameter ljust "minimum" length of string. pad spaces if value longer current length of string.

unless total_section_len 1, 60//(total_section_len * 5) less length of strings, no padding taking place.

also, doesn't modify string in place strings immutable. need use return value, i.e. temp = temp.ljust(...)


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -