简体   繁体   中英

Right Align Text File

Trying to right align the 2nd set of strings. What are some other methods that I could use so that the characters in the first string doesn't affect the format of the second string?


This is what I have so far.

count = 0 for line in inputFile:

%(total) + "\\n")

    for lis)

You can apply string formatting to right justify like this:

my_list = ['A12345678', "Adams, John", 14.98]
print("{0} {1:>20} {2:}".format(my_list[0], my_list[1], my_list[2]))

Where the {0} , {1} , and {2} specify position of the arguments, and the :>20 denotes that you want to ensure a length of 20 characters (right justified) for that item.

Output:

A12345678          Adams, John 14.98

So just decide how many characters you expect to be printing at max and then substitute that number for the 20 in my code!

For more string formatting methods for both the % and .format() methods check out https://pyformat.info/

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