简体   繁体   中英

Python - formatting a tuple/string

For my Coursework which I am desperately struggling with I have tried to set my inputs into a dictionary and then use this to format and print the string so that it is displayed as shown below.

 Surname, Forename Payroll Department Salary 

The name should be displayed using the format shown above so you will need to create a string containing the name in this format and print it in a fixed-width field. You may assume that no name will contain more than 30 characters when displayed in this format, no department will contain more than 15 characters, payroll numbers will contain at most 5 digits and all salaries will be integers less than 100,000.

So far I have only managed this as every time I seem to alter any of line 9it comes back with an error saying that "tuple indices must be integers or slices, not strings" but I have no idea how to do this.

payroll = int(input("Enter your Payroll.")) 
department = input("Enter your Department Name.") 
salary = int(input("Enter your Salary."))
forename = input("Enter your Forename.")
surname = input("Enter your Surname.")

list_lect = payroll, department, salary, forename, surname

str = '{0[4]},{0[3]}{0[0:5]} {0[2]}    {0[3]}'.format(list_lect)

print(str)

Any help would be much appreciated from someone struggling with python.

While it would be simple to make a print function to print the way you want:

a = ('Surname', 'Forename', 'Payroll', 'Department', 'Salary')
def printer(tup):
        print_string = str("(")
        pad = 24
        print_string += ", ".join(tup[:2]).ljust(pad)
        print_string += ", ".join(tup[2:4]).ljust(pad)
        print_string += tup[-1] + ")"
        print(print_string)

>>> printer(a)
(Surname, Forename       Payroll, Department     Salary)

I would suggest that it would be cleaner to handle this a different way. Perhaps might I recommend taking in the values separately and then combining them in a named way. Like this

payroll = input("Enter your Payroll.") 
department = input("Enter your Department Name.") 
salary = input("Enter your Salary.")
forename = input("Enter your Forename.")
surname = input("Enter your Surname.")

You can then perform which ever grouping you want and print them in a more sane manner

print("%s, %s      %s, %s     %s" % (surename, forename, .....etc)

and then you can store them in a data structure that makes sense as well

Why do you need this? Printing a tuple with spacing is impossible to my knowledge, but I'm sure theres another way to achieve what you're looking for. Aside from that, there is a kind of work around, although you aren't printing a tuple, to say.

indexs = {
    payroll = 0,
    dept = 1,
    salary = 2,
    name = 3,
    surname = 4
}
str = "('{surname}', '{name}'      '{payroll}', '{dept}'      
'{salary}')".format(surname = z[indexs[surname]], name = z[indexs[name]], 
payroll = z[indexs[payroll]], dept = z[indexs[dept]], salary = 
z[indexs[salary]])
print(str)

Its not perfect, as its just string formatting, but if you want your output to look exactly as you said, this is the only way. Of course your aim might be very different. Anyway, hope this helps

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