Archive

Posts Tagged ‘variable width’

print a text by specifying the width via a variable

July 11, 2014 Leave a comment

Problem
You want to print a text using a certain width that you want to specify via a variable. Running example:

$ ./test.py 
Width: 10
'     World'
Width: 20
'               World'

Solution

for _ in range(2):
    width = int(raw_input("Width: "))
    print("'{0:>{w}}'".format("World", w=width))

The sign ‘>‘ can be omitted. It simply means “align to the right”.

Tip from here.

Categories: python Tags: ,
Design a site like this with WordPress.com
Get started