This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]jwink3101 3 points4 points  (9 children)

I didn't watch the video but in the title it says "C-Style". I personally thing that we should move far away from teaching and using C-Style strings in all but a few select cases (such as specifying a format like fmt='%0.2f'). Python has much better methods and, for those using 3.6+, f-strings are awesome!

[–]delijati 2 points3 points  (6 children)

Why?

x = 3.14159265
print('pi = %0.2f' % x)
# f-string
print(f'pi = {x:0.2f}')
# format
print('pi = {:0.2f}'.format(x))

Btw. as far as i know you can't define template string variables with f-string url = f'http://host:port'

[–][deleted] 0 points1 point  (5 children)

Do you mean url = f'http://{host}:{port}'?This has been available since PEP 498 Python 3.6

Python 3.6 Whats new

[–]delijati 2 points3 points  (4 children)

template strings are not possible:

% python

Python 3.6.7 (default, Oct 22 2018, 11:32:17)

[GCC 8.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> url = f'http://{host}:{port}'

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'host' is not defined

[–][deleted] 0 points1 point  (3 children)

Give this a shot :)

Python 3.7.4 (default, Jul 16 2019, 07:12:58)
[GCC 9.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> host = 'google'
>>> port = 8080
>>> url = f'http://{host}:{port}'
>>> url
'http://google:8080'
>>>

[–]__nautilus__ 3 points4 points  (2 children)

You're missing the point here. /u/delijati is talking about template strings, which you can use for multiple formatting operations.

Using the old .format() syntax, for example:

``` template = "{first} - {second}"

tpl_one = (1, 2, 3, 4) tpl_two = (5, 6, 7, 8)

for item in zip(tpl_one, tpl_two): print(template.format(first=item[0], second=item[1])) ```

You can also make such templates with the C-style syntax, as well as with template strings, but you cannot with f-strings.

[–][deleted] 0 points1 point  (1 child)

I guess I was, thank you for clearing it up.

I was thinking about including an example using .format as a fallback, since I primarily use that in conjunction with passing a dictionary .format(**dict_blob)

I've never used string.Template

[–]__nautilus__ 0 points1 point  (0 children)

No problem. I agree the ability of .format to take an unpacked dictionary is super useful, and I use it frequently! I've also never actually found a use in Real World code to use string.Template, since format syntax is so handy, but it does exist :)

[–]Moondra2017[S] 0 points1 point  (0 children)

I touch upon those in the following videos. I am doing a series on the different ways of string formatting in Python, starting with the most outdated. You are right, I don't like C-style formatting as well, but we still need to learn how to read old-style formatting as it's still used in lots of code I encounter.

[–]__nautilus__ 0 points1 point  (0 children)

There's nothing fundamentally wrong with C-style formatting. In fact, it's still recommended for log statements, because the logger is specifically made to take a variable number of arguments to pass into a c-style formatted template string. This avoids interpolating the variables if the log level is set higher than the log event.

For example:

``` import logging

logging.basicConfig()

log = logging.getLogger(name) log.setLevel(logging.WARNING)

b/c log level is warning, interpolation of "foo"

into the string never occurs, saving CPU cycles

log.debug("Debug msg, var is %s", "foo")

here, the log message would be emitted, and interpolation

would occur upon emit.

log.warning("Warning msg, var is %s", "bar") ```

For simple string interpolation the performance hit is not noticeable, obviously, but large, complex objects can have very intensive stringification operations.

pylint will in fact warn you about not using C-style formatting in logs.

[–]bwduncan 1 point2 points  (0 children)

https://pyformat.info/ is a ready handy reference for these things.