-1

I would like to output something like:

===>>> [FINISHED] Building sources: bla                               (1h:20m:30s)
===>>> [FINISHED] Building sources: The brown fox jumped...           (7h:05m:00s)

That is, a string filling a width of N characters, with a first part left-aligned, and a second part right-aligned.

I have a print in a function and I just want to get an easy-to-read output. At the end of the execution of the script, I will see a few lines like the ones above.

Some more comments:

  • The two parts of the string together are never going to exceed N.
  • N is a constant value.
  • I've already got the two strings, I don't need any date formatting.

Is it posible to do this using Python's string `format ìn a generic way? Something like:

N=80
first_part_of_the_string  = "===>>> [FINISHED] Building sources: " + some_random_text
second_part_of_the_string = my_function_to_convert_time_to_hms(time)

print("{<}{:N>}".format(first_part_of_the_string, second_part_of_the_string))
rturrado
  • 7,699
  • 6
  • 42
  • 62
  • 1
    I would appreciate any comments from the the down voters, thanks! :) – rturrado Aug 23 '16 at 12:03
  • 1
    I did not downvote, but I think the issue others are having with this question is that you don't seem to have followed the best practices for asking a question: http://stackoverflow.com/help/how-to-ask. Namely, you don't show that you have attempted to solve the problem on your own, or researched it yourself before posting. I agree that downvoting without feedback is not very useful. – elethan Aug 23 '16 at 12:08
  • I agree with elethan, and also your specification is not clear. Do you just want to print, or do you want a function that returns a string that you can print? What should be done if the two strings combined have more than `N` characters? Are the two 'strings' already strings, or do you need to convert a datetime value to a string first? Is `N` fixed or could it change? And so on. – Rory Daulton Aug 23 '16 at 12:09
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Aug 23 '16 at 12:10
  • @elethan - Ah OK, thanks! It looks like, yes, you're right. In fact, I didn't fight much to get a solution. I tried `rjust`, and I read a little about `format` but I didn't find an easy solution. I thought though that I would get a quick and simple solution from somebody here. – rturrado Aug 23 '16 at 12:11
  • @RoryDaulton Thanks! Editing my question with your comments. – rturrado Aug 23 '16 at 12:18
  • @TigerhawkT3 Sorry to give you that impression. I've asked many times in SO and believe me I never ask before trying. As I said though, I didn't try very hard to find a solution this time, I just wanted to keep the question simple and clear and that's why I didn't give much information. But you're all right, it looked as if I just wanted some work to be done for me. Sorry about that! – rturrado Aug 23 '16 at 12:24

2 Answers2

2

assuming you know the width of the line you can use '{:n}'.format(string) to fill the string up to n-length with spaces. This does not shorten the string if it is longer than n, as you state will never be the case.

'===>>> [FINISHED] Building sources: {:35} ({})'.format('bla', 'time')

in a similar fashion you can format the time by padding with zeroes: {:02}

hour = 1
minute = 20
second = 30
prefix = '===>>> [FINISHED] Building sources: '
content = 'bla'

time = '({:02}h:{:02}m:{:02}s)'.format(hour, minute, second)
print '{}{:35} {}'.format(prefix, content, time)

printing

'===>>> [FINISHED] Building sources: bla                                 (01h:20m:30s)'
Laurens Koppenol
  • 2,946
  • 2
  • 20
  • 33
  • 1
    What if a time has 10 or more hours? – Stefan Pochmann Aug 23 '16 at 12:09
  • @LaurensKoppenol Thanks. So let's say N=80, as you say there is a text in every line that is fixed (I've counted 49 characters with blanks), your solution is to limit the variable string to 31. Is that correct? – rturrado Aug 23 '16 at 12:36
  • 1
    that is correct. Within the format you can write 'bla'[0:31] to prevent strings longer than 31 chars to spoil the fun: .format(string[0:31], time) – Laurens Koppenol Aug 23 '16 at 12:55
  • @LaurensKoppenol It sounds good. For accepting an answer though I'll wait to see if there is a more generic way to do this, using Python's `format` for example, with a piece of code as I wrote at the end of the question. – rturrado Aug 23 '16 at 17:24
  • it feels like i'm doing your homework for you, but check updated answer – Laurens Koppenol Aug 24 '16 at 06:25
0

Just one way, adding the right number of spaces...

>>> for a, b in ('fsad', 'trwe'), ('gregrfgsd', '5435234523554'):
        print a + ' ' * (50 - len(a + b)) + b

fsad                                          trwe
gregrfgsd                            5435234523554
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107