Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When not to use f-strings?
#6
(Sep-16-2019, 04:54 AM)Gribouillis Wrote: This is not really an answer, but don't forget that templating systems also exist for "complex" string formatting, such as Mako templates. For me, fstrings are only designed for simple string interpolations, in the same category as format() or %.
Hi!
Thank you for your answer. I didn't know about Mako templates (as I don't know many other things in Python Blush ).

I checked and it seems that it is used by reddit.com, for example.
The problem is that I'm just starting, and it uses classes, something that I don't know how to use yet. But I don't have problems printing complex f-string formatting. I DO have problems when trying to time the printing of that complex f-string.

For instance, for very simple strings:
import timeit

integer1 = 25
string1 = 'TWENTY-FIVE'

setup = """
integer1 = 25
string1 = 'TWENTY-FIVE'
""".strip()

print('\nString number %s, or simply %d' % (string1, integer1))
print('String number {}, or simply {}'.format(string1, integer1))
print(f'String number {string1}, or simply {integer1}')

percent_stmt = "'String number %s, or simply %d' % (string1, integer1)"
call_stmt = "'String number {}, or simply {}'.format(string1, integer1)"
fstr_stmt = """f'String number {string1}, or simply {integer1}'"""

def time(stmt):
    return f"{timeit.timeit(stmt, setup, number=int(1e4)):.5f}"

print(f"\nThe printing times of the different formatting types are:\n")
print(f"Timing percent formating:      {time(percent_stmt)}s" )
print(f"Timing call formating:         {time(call_stmt)}s")
print(f"Timing f-string formating:     {time(fstr_stmt)}s\n")
and that produced on one of the occasions, the following ouput:
Output:
String number TWENTY-FIVE, or simply 25 String number TWENTY-FIVE, or simply 25 String number TWENTY-FIVE, or simply 25 The printing times of the different formatting types are: Timing percent formating: 0.00296s Timing call formating: 0.00333s Timing f-string formating: 0.00147s
or even with a bit longer strings:

import timeit

name = "Arnold Schwarzenegger"
age = 72

print("\n\nThis is printing strings with %-formatting (not recommended by the Python docs):") 
print('%s is %s.' % (name, age))
t1 = timeit.timeit("""name = "Arnold Schwarzenegger"
age = 72
'%s is %s.' % (name, age)""", number = 10000)
print("That took", t1, end='s to print.')

print("\n\nThis is printing strings with str.format():")
print('{} is {}.'.format(name, age))
t2 = timeit.timeit("""name = "Arnold Schwarzenegger"
age = 72
'{} is {}.'.format(name, age)""", number = 10000)
print("That took", t2, end='s to print.')

print("\n\nThis is printing strings with f-formatting:")
print(f'{name} is {age}.')
t3 = timeit.timeit("""name = "Arnold Schwarzenegger"
age = 72
f'{name} is {age}.'""", number = 10000)
print("That took", t3, end='s to print.\n')

print(f"""
It seems that {t3}s (f-formatting) is < {t1}s (%-formatting)
(%-formatting is not recommended by the Python docs).

It seems also that {t3}s (f-formatting) is < {t2}s (str.format()).

So it appears clearly that F-FORMATTING IS INDEED THE FASTEST WAY TO PRINT STRINGS.
   """)
which in one of the instances, produced this output:
Output:
This is printing strings with %-formatting (not recommended by the Python docs): Arnold Schwarzenegger is 72. That took 0.0026088639999999885s to print. This is printing strings with str.format(): Arnold Schwarzenegger is 72. That took 0.00298629899999997s to print. This is printing strings with f-formatting: Arnold Schwarzenegger is 72. That took 0.0014626989999999562s to print. It seems that 0.0014626989999999562s (f-formatting) is < 0.0026088639999999885s (%-formatting) (%-formatting is not recommended by the Python docs). It seems also that 0.0014626989999999562s (f-formatting) is < 0.00298629899999997s (str.format()). So it appears clearly that F-FORMATTING IS INDEED THE FASTEST WAY TO PRINT STRINGS.
but when the strings are more complex, even not having problems at printing them, I'm having problems at timing the printing of those examples.

(Sep-16-2019, 04:54 AM)Gribouillis Wrote: For me, fstrings are only designed for simple string interpolations, in the same category as format() or %.
I was trying to find out where I saw that %-formatting is going to be deprecated (I thought it was in the Python docs, but I cannot find it now, although they referred to it, as the old %-formatting). Huh

All the best,

(Sep-16-2019, 06:20 AM)perfringo Wrote:
(Sep-16-2019, 06:09 AM)buran Wrote: just to mention that if there is need for escaping within placeholder than it's a case of having expression IN the placeholder. In this case it would be better and more readable to have the expression prepared and assigned to a variable beforehand and use that variable in the f-string

To showcase buran's suggestion:

>>> newline = '\n'                                                        
>>> first = 'first'                                                       
>>> second = 'second'  
>>> print(f'{first}{newline}{second}')
first
second
Thank you to both of you, Buran and Perfringo!

What I meant is this, by modifying your program:

import timeit

newline = '\n'
first = 'first'
second = 'second'
print("\n\nThis is printing strings with f-formatting:")
print(f'{first}{newline}{second}')
t3 = timeit.timeit("""newline = '\n'
first = 'first'
second = 'second'
f'{first}{newline}{second}'""", number = 10000)
print("That took", t3, end='s to print.\n')
And when I run it, I have this output with an error message:
Output:
This is printing strings with f-formatting: first second
Error:
Traceback (most recent call last): File "C:/Users/User1/AppData/Local/Programs/Python/Python37/compare_speed_strings_03.py", line 15, in <module> f'{first}{newline}{second}'""", number = 10000) File "C:\Users\User1\AppData\Local\Programs\Python\Python37\lib\timeit.py", line 232, in timeit return Timer(stmt, setup, timer, globals).timeit(number) File "C:\Users\User1\AppData\Local\Programs\Python\Python37\lib\timeit.py", line 121, in __init__ compile(stmtprefix + stmt, dummy_src_name, "exec") File "<timeit-src>", line 2 newline = ' ^ SyntaxError: EOL while scanning string literal
1) Is it something that I'm doing wrong? (with my other example programs that I attached in my previous posts, I didn't have problems)
2) Is it something with f-strings and newlines to be timed? (no problems to print)

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Messages In This Thread
When not to use f-strings? - by newbieAuggie2019 - Sep-16-2019, 02:46 AM
RE: When not to use f-strings? - by Gribouillis - Sep-16-2019, 04:54 AM
RE: When not to use f-strings? - by newbieAuggie2019 - Sep-16-2019, 06:31 AM
RE: When not to use f-strings? - by perfringo - Sep-16-2019, 05:56 AM
RE: When not to use f-strings? - by buran - Sep-16-2019, 06:09 AM
RE: When not to use f-strings? - by perfringo - Sep-16-2019, 06:20 AM
RE: When not to use f-strings? - by snippsat - Sep-16-2019, 08:54 AM
RE: When not to use f-strings? - by DeaD_EyE - Sep-16-2019, 08:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 1,928 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 2,733 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 3,380 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 5,136 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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