Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
276 views
Welcome To Ask or Share your Answers For Others

1 Answer

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:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...