Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center text with variable python3.6

First, I am new to programming in general, so that means I'm new to python as well, so type slowly so I can keep up. I am trying to center a line of text that includes a variable that was input by a user. Something like this:

age = input ("How old are you? ")    
print ("{: ^80}".format("You are", (age), "years old."))

I've tried as many ways as I can think of to get this to center the entire line reading . . . You are 40 years old. (Assuming, of course, the user input = 40) with no luck. What am I doing wrong?

Thanks, Robert

like image 578
PredsFan Avatar asked Jun 21 '26 20:06

PredsFan


1 Answers

Using .center() on strings will pad them with spaces on both sides.

print('You are {} years old'.format(age).center(80))
like image 150
Hadus Avatar answered Jun 24 '26 12:06

Hadus