Sep-16-2019, 02:46 AM
Hi!
I knew nothing about f-strings until on this site somebody suggested them as a more appropriate and straightforward way to print strings. The books I was using didn't say anything about them, as f-strings appeared with Python 3.6. Such is the case, that even in books printed in 2018 and 2019, you are lucky if more than a paragraph is dedicated to them!!!
So I looked for sites on the internet, and I found more information. I did even write a couple of simple programs comparing the printing time for strings printed with the %-format, strings printed with the .format() type, and f-strings. The f-string formatting seems to be the fastest, at least when the strings are simple. The second place is for %-formatted strings, which is curious, as they seem to be deprecated in the near future. I think I read it's due to some collateral problems... Not sure which ones...
Then, I decided to complicate things a bit more, with multiline strings with even lines in blank, like citing a few quotes attributed to somebody. While I had no problems at printing them, I encountered problems at trying to time them (I tried different functions from different modules, with similar problems). It seems that then, f-strings is not very new line (\n) friendly, and although I tried some suggestions I found, they didn't work. They were too many quotation marks; I tried adding quotation marks used in other languages, but it was of no use either. I tried using strings as a concatenation of strings, I tried also to make one of those concatenated strings equal to "\n", but neither of them managed to work...
So, my questions are:
1) When it's better not to use f-strings?
2) As my purpose was to compare the printing time for complex strings formatted differently, is there a way to do it, or it cannot be used for f-strings?
All the best,
I knew nothing about f-strings until on this site somebody suggested them as a more appropriate and straightforward way to print strings. The books I was using didn't say anything about them, as f-strings appeared with Python 3.6. Such is the case, that even in books printed in 2018 and 2019, you are lucky if more than a paragraph is dedicated to them!!!
So I looked for sites on the internet, and I found more information. I did even write a couple of simple programs comparing the printing time for strings printed with the %-format, strings printed with the .format() type, and f-strings. The f-string formatting seems to be the fastest, at least when the strings are simple. The second place is for %-formatted strings, which is curious, as they seem to be deprecated in the near future. I think I read it's due to some collateral problems... Not sure which ones...
Then, I decided to complicate things a bit more, with multiline strings with even lines in blank, like citing a few quotes attributed to somebody. While I had no problems at printing them, I encountered problems at trying to time them (I tried different functions from different modules, with similar problems). It seems that then, f-strings is not very new line (\n) friendly, and although I tried some suggestions I found, they didn't work. They were too many quotation marks; I tried adding quotation marks used in other languages, but it was of no use either. I tried using strings as a concatenation of strings, I tried also to make one of those concatenated strings equal to "\n", but neither of them managed to work...
So, my questions are:
1) When it's better not to use f-strings?
2) As my purpose was to compare the printing time for complex strings formatted differently, is there a way to do it, or it cannot be used for f-strings?
All the best,