J.C
J.C

Reputation: 752

String Formatting in python remove whitespace before : in most inefficient way

I want to get this output. AssertionError: a mystery exception! However I get this output AssertionError : a mystery exception! I want to remove the space before ":" what is the best way to do so. Thank you very much

def fonction(n):
    try:
        print(mystery(n))
    except Exception as err:
        print(type(err).__name__,":",err)

Upvotes: 1

Views: 88

Answers (2)

Nurjan
Nurjan

Reputation: 6053

If you're using python3.6 you can format string this way as well:

print(f'{type(err).__name__}: {err}')

This is called f-string and is a new way of formatting strings.

Upvotes: 0

blhsing
blhsing

Reputation: 106553

You can use the string formatting operator:

print('%s: %s' % (type(err).__name__, err))

or the str.format method:

print('{}: {}'.format(type(err).__name__, err))

Upvotes: 4

Related Questions