Nov-13-2021, 11:55 PM
In the example below, both prints produce the same output:
import socket HEADERSIZE=10 msg = "Welcome to the server!" print(f'{len(msg):<10{HEADERSIZE}}+msg') # {HEADERSIZE} will be replaced by 10 print(f'{len(msg):<1010}+msg')So this makes a string that starts with len(msg) followed lots and lots of padding and finally "Welcome to the server!"
msg = f'{len(msg):<10{HEADERSIZE}}'+ msgThe reason you got an error trying to evaluate "len(msg):<10" is that this is not Python code, it is f'string formatting code. Just like the syntax for regular expressions is not Python, the format syntax for f'strings is not Python. These are special purpose mini-languages for doing specific tasks.