简体   繁体   中英

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)

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))

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.

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