简体   繁体   中英

syntax error with .format() (Python 3.2.3)

So I am trying to write a program that writes a word, fills 20 spaces with markup(< or >), then writes that same word backwards. It also takes user input. Can anyone tell me what im doing wrong

 while test2 == 1 : 
    test = input("Enter a word to format. Entering QUIT will exit the program:")
    if test == ("quit"):
        print("You have quit the program.")
        break
    else:
        print("{0:*<20}{1:*>20})".format(
            "test")(["test"::-1])

Syntactically you have a " in the wrong place in your last print statement.

print("{0:*<20}{1:*>20}").format("test")(["test"::-1])
                       ^

You also some other syntax errors with your format() arguments, this will work for the literal string "test" but you'll need to replace it with a variable to use it with actual user input:

print("{0:*<20}{1:*>20}").format("test","test"[::-1])

Your last line has errors, try the follwing to get 20 spaces between the words

In [1]: s = "test"
In [2]: print('{0:<20}{1}'.format(s,s[::-1]))

test                tset

You can also use print('{0}{1}{2}'.format(s," "*20,s[::-1])) which is subjectively cleaner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM