0

I try to print to print a space with Specification Mini-Language with using variables.

for example:

instead of this

print('{:>10}'.format('hello'))

I want do this:

i = 10
print('{:>i}'.format('hello'))

I get this error:

ValueError: Unknown format code 'i' for object of type 'str'

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
William Kh
  • 30
  • 6

3 Answers3

2

Put i in brackets and add it to the format arguments like this:

i = 10
print('{:>{i}}'.format('hello', i=i))
#      hello
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
0

Could do it like this:

print(('{:>' + str(i) +'}').format('hello'))
John Coleman
  • 51,337
  • 7
  • 54
  • 119
0

You can create your own format string like this:

i = 10
format_string = '{:>' + str(i) + '}'
print(format_string .format('hello'))
quamrana
  • 37,849
  • 12
  • 53
  • 71