Basic Python help
fredag 23 oktober 2020 kl. 13:22:55 UTC+2 skrev Frank Millman:
> On 2020-10-23 12:41 PM, mikael petterson wrote:
> > Hi,
> > I need to use the following code but in java.
> > END_DELIM = '\n##\n'
> > def start_delim(data_len): return '\n#%s\n' % (data_len)
> > data = "%s%s%s" % (start_delim(len(data)), data, END_DELIM)
> > Can anyone help me to understand what it means:
> > I am guessing now:
> > a function defined "start_delim" takes the length of a data string.
> > function does modulo on something. This something I am not sure of
> > :-)
> > Does '\n#%s\n' will this be evaluated to a number when %s i replaced with data_len?
> > Then the result is used as one parameter in "%s%s%s"
> > start_delim then for the other
> > data
> > END_DELIM
> I think it is simpler than that.
> >>> '\n#%s\n' % 2
> All it is doing is replacing '%s' with the length of the string.
> So the result is the concatenation of -
> 1. '\n' + '#' + length of string + '\n' as the start delimiter
> 2. the string itself
> 3. '\n' + '#' + '#' + '\n' as the end delimiter
> Frank Millman
Yes that was simpler than I thought. Thanks a lot!