-1

I have a string I am formatting and printing using f-string and I need to eliminate some spaces. Here is my code:

if len(probably) > 0 and len(might) > 0:  
       print(f'{name}:',f'Might({might})', f'Probably({probably})')
    elif len(probably) == 0 and len(might) > 0:  
       print(f'{name}:',f'Might({might})')
    elif len(probably) > 0 and len(might) == 0:
       print(f'{name}:',f'Probably({probably})')
    elif len(probably) == 0 and len(might) == 0:
       print(f'{name}:')

and here is what it looks like currently:

1: Might(4, 6, 7) Probably(11)
2: Might(5, 8, 10) Probably(9)
3: Might(4, 5, 6, 7, 8, 12)
4: Might(1, 3, 6, 11, 12)
5: Might(2, 3, 8, 10) Probably(9)
6: Might(1, 3, 4, 7, 11)
7: Might(1, 3, 6)
8: Might(2, 3, 5, 9, 10)
9: Might(8, 10) Probably(2, 5)
10: Might(2, 5, 8, 9)
11: Might(4, 6) Probably(1)
12: Might(3, 4)
13:

and here is what I need it to look like

    1:Might(4,6,7) Probably(11)
    2:Might(5,8,10) Probably(9)
    3:Might(4,5,6,7,8,12)
    4:Might(1,3,6,11,12)
    5:Might(2,3,8,10) Probably(9)
    6:Might(1,3,4,7,11)
    7:Might(1,3,6)
    8:Might(2,3,5,9,10)
    9:Might(8,10) Probably(2,5)
    10:Might(2,5,8,9)
    11:Might(4,6) Probably(1)
    12:Might(3,4)
    13:
Thesqlkid
  • 27
  • 6
  • `might` and `probably` are what types? lists? – astrochun Feb 18 '21 at 00:34
  • I think you changed the expected format @martineau – astrochun Feb 18 '21 at 00:35
  • @astrochun: I only edited the title of the question — so that cannot be true. – martineau Feb 18 '21 at 00:38
  • "Trim spaces" where? Everywhere? At the ends? It's not super clear what's going on with your expected/actual outcomes. An f-string is still just a string at the end of the day: to **strip** whitespace from both ends of a string: `str.strip()`. – ddejohn Feb 18 '21 at 00:40
  • Sorry yes might and probably are lists and I see I did mess up the formatting with the first 3 in what I need it to look like. I need to remove spaces between the colon and the first word (might or probably), I need to remove the spaces after the commas between numbers in the might and probably formats. – Thesqlkid Feb 18 '21 at 00:52
  • OK, @martineau. I'm confused then. How does he want a space after "3: Might..." and then "4:Might..." – astrochun Feb 18 '21 at 00:57
  • 1
    @astrochun: I have no idea and didn't touch the body of the OP's question — so suggest you ask _them_ to clarify. – martineau Feb 18 '21 at 01:02
  • OK, so they are lists, but here's the thing, how is it that you don't have square brackets. I would have expected: `Might:[3, 4, 5]` with `print(f"Might:{test}")` – astrochun Feb 18 '21 at 01:06
  • @Thesqlkid can you please edit the OP to reflect exactly the output you want? You're causing quite a bit of confusion. – ddejohn Feb 18 '21 at 01:11
  • In the future you should also provide all the code necessary to reproduce the output you're getting, this includes however you're defining `name` and however you're changing the values of `might` and `probably`. – ddejohn Feb 18 '21 at 01:27
  • got it. thanks for your help everyone. – Thesqlkid Feb 18 '21 at 01:51

1 Answers1

1

An f-string is still just a string at the end of the day:

>>> x = "hello"
>>> print(f"    {x}    ".strip())
hello

However, it's not quite clear what you expect your output to look like, as the spacing seems inconsistent between the number and the Might on each line:

3: Might(4,5,6,7,8,12)
4:Might(1,3,6,11,12)

Why is there a space in one and not the other? To add: you can take advantage of the truthiness of your objects and simplify your if-statements:

if probably and might:  
    print(f'{name}:', f'Might({might})', f'Probably({probably})')
elif not probably and might:  
    print(f'{name}:', f'Might({might})')
elif probably and not might:
    print(f'{name}:', f'Probably({probably})')
elif not probably and not might:
    print(f'{name}:')

If you want to get wild with truthiness, you can get rid of the if-statements entirely:

print(f"{name}:", f"Might({might})"*bool(might), f"Probably({probably})"*bool(probably))

EDIT

Since you've added some context via a comment, here's what you can do:

# Get rid of spaces between numbers
might_str = ",".join(map(str, might))
prob_str = ",".join(map(str, probably))

if probably and might:  
    print(f'    {name}:Might({might_str}) Probably({probably_str})')
elif not probably and might:  
    print(f'    {name}:Might({might_str})')
elif probably and not might:
    print(f'    {name}:Probably({probably_str})')
elif not probably and not might:
    print(f'    {name}:')

Some output including the leading spaces that you have in your "expected output":

    1:Might(1,2,3,4,5) Probably(5,6,2,3,1)
    2:Probably(5,6,2,3,1)
    3:Might(1,2,3,4,5)
    4:
ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • I don't think `"".join(might)` will entirely work. He wants commas in between. But keep in mind that this will not work if `might` is a list of int and not str. – astrochun Feb 18 '21 at 01:09
  • The reason you're seeing spaces is because `print(a, b, c)` by default places a whitespace between each value a, b, and c that gets printed. You can get around this by fiddling with the `print()` function's keyword arguments, OR, you can just use a single f-string to do what you want exactly. – ddejohn Feb 18 '21 at 01:09