Nov-14-2021, 03:26 AM
(Nov-13-2021, 11:55 PM)deanhystad Wrote: 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.
if I write : f'{msg:<1010}' the first 10 stands for 10 charcaters and msg is inside those 10 characters, but what does the second 10 stands for? Does it multiply the first 10, because that's what it looks like when i put it in python