info = (data[:75] + '..') if len(data) > 75 else data
This code matches the JavaScript, but you should consider using data[:73] so that the total result including the .. fits in 75 characters.
info = (data[:75] + '..') if len(data) > 75 else data
This code matches the JavaScript, but you should consider using data[:73] so that the total result including the .. fits in 75 characters.
Even more concise:
data = data[:75]
If it is less than 75 characters there will be no change.
python - Split string into strings by length? - Stack Overflow
[Python] How do I slice and print half of a string regardless of how long it is?
How to quickly reformat very long strings in python code using elpy+flycheck while maintaining PEP8-compliance?
Hey, this is not a direct answer to your question but I'd like to point out a different python package that might help you with python formatting https://github.com/psf/black
Black is an autoformatter that will modify all of your python code so that it looks consistent. It is very opinionated about how your code should look so that you don't have to be. I think it gets the formatting right most of the time and in the cases I disagree I just let it do its thing because its still consistent. Using black has allowed me to stop worrying about formatting entirely. There is also an emacs package to interface with it: https://github.com/pythonic-emacs/blacken
It sadly does not wrap and split long strings and comment, though it will wrap and split expressions
More on reddit.comHow to remove everything after a certain character?
>>> x = "qwertyui"
>>> chunks, chunk_size = len(x), len(x)//4
>>> [ x[i:i+chunk_size] for i in range(0, chunks, chunk_size) ]
['qw', 'er', 'ty', 'ui']
- :param s: str; source string
- :param w: int; width to split on
Using the textwrap module:
PyDocs-textwrap
import textwrap
def wrap(s, w):
return textwrap.fill(s, w)
:return str:
Inspired by Alexander's Answer
PyDocs-data structures
def wrap(s, w):
return [s[i:i + w] for i in range(0, len(s), w)]
- :return list:
Inspired by Eric's answer
PyDocs-regex
import re
def wrap(s, w):
sre = re.compile(rf'(.{{{w}}})')
return [x for x in re.split(sre, s) if x]
- :return list: