I am currently writing a short program that performs frequency analysis. However, there is one line that bothers me:
"{0[0]} | " + "[]" * num_occurrences + " Total: {0[1]!s}"
Is there a way in Python to repeat certain characters an arbitrary number of times without resorting to concatenation (preferably inside a format string)? I don't feel like doing it in the most pythonic way.
The best way to repeat a character or string is to multiply it:
>>> "a" * 3 'aaa' >>> '123' * 3 '123123123'
And for your example, I would probably use:
>>> "{0[0]} | {1} Total: {0[1]!s}".format(foo, "[]" * num_occurrences)
Source: https://habr.com/ru/post/1216363/More articles:Format number in jsx React component - javascriptHow to declare an array in VBScript? - arrayspython opencv videowrite not recording video - pythonFind the most common line in a file in bash - linuxScala: how to mask the first N characters of a string - scalaSwift Stable Sort? - sortingUndefined reference to `__cxa_thread_atexit @@ CXXABI` when compiling with` libะก ++` on linux - c ++Parsing "xyz" with multiplication priority - f #Examples of template metaprogramming over constexpr? - c ++How to use the same scanner for multiple classes in Java - javaAll Articles