Python Forum
trying to understand a string literal in a basic server program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to understand a string literal in a basic server program
#4
(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}}'+ msg
The 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
Reply


Messages In This Thread
RE: trying to understand a string literal in a basic server program - by CompleteNewb - Nov-14-2021, 03:26 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 684 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  How to take the tar backup files form remote server to local server sivareddy 0 1,991 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  PLEASE HELP! Basic Python Program joeroffey 5 2,540 Apr-10-2021, 02:59 PM
Last Post: jefsummers
  EOF while scanning triple-quoted string literal louis216 1 4,052 Jun-30-2020, 04:11 AM
Last Post: bowlofred
  invalid literal for int() with base 10: '' mrsenorchuck 5 5,860 Apr-29-2020, 05:48 AM
Last Post: markfilan
  Python basic program webshakeup 4 2,874 Sep-19-2019, 07:14 AM
Last Post: Maheshsharma
  Syntax Error EOL while scanning string literal intjuma 0 2,536 Jun-28-2019, 01:56 AM
Last Post: intjuma
  Heating program to translate from Basic to Python tommy2k19 12 6,189 May-21-2019, 08:16 AM
Last Post: tommy2k19
  Very Basic String Manipulation gstu 2 2,744 Mar-18-2018, 03:08 PM
Last Post: gstu
  SyntaxError: EOL while scanning string literal on with open() statement Regulus 3 7,463 Feb-23-2018, 06:40 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020