string-formatting

f-string syntax for unpacking a list with brace suppression

f-string syntax for unpacking a list with brace suppression Question: I have been examining some of my string format options using the new f-string format. I routinely need to unpack lists and other iterables of unknown length. Currently I use the following… >>> a = [1, ‘a’, 3, ‘b’] >>> (“unpack a list: ” + …

Total answers: 5

Using !s vs. :s to format a string in Python

Using !s vs. :s to format a string in Python Question: I’m really curious about :s format string in Python 3. The documentation says !s is conversion and that :s is format_spec. It also says !s will apply str(), but it doesn’t say anything similar about :s. I think there’s no significant difference between them, …

Total answers: 3

Nested f-strings

Nested f-strings Question: Thanks to David Beazley’s tweet, I’ve recently found out that the new Python 3.6 f-strings can also be nested: >>> price = 478.23 >>> f”{f’${price:0.2f}’:*>20s}” ‘*************$478.23′ Or: >>> x = 42 >>> f”’-{f”””*{f”+{f’.{x}.’}+”}*”””}-”’ ‘-*+.42.+*-‘ While I am surprised that this is possible, I am missing on how practical is that, when would …

Total answers: 13

How to use str.format inside a string of json format?

How to use str.format inside a string of json format? Question: Python Version 3.5 I’m trying to make an API call to configure a device using json as the format. Some of the json will vary depending on the desired naming, so I need to call a variable in the string. I am able to …

Total answers: 1

use .format() in a string in two steps

use .format() in a string in two steps Question: I have a string in which I want to replace some variables, but in different steps, something like: my_string = ‘text_with_{var_1}_to_variables_{var_2}’ my_string.format(var_1=’10’) ### make process 1 my_string.format(var_2=’22’) But when I try to replace the first variable I get an Error: KeyError: ‘var_2’ How can I accomplish …

Total answers: 3

Format strings vs concatenation

Format strings vs concatenation Question: I see many people using format strings like this: root = “sample” output = “output” path = “{}/{}”.format(root, output) Instead of simply concatenating strings like this: path = root + ‘/’ + output Do format strings have better performance or is this just for looks? Asked By: wjk2a1 || Source …

Total answers: 7

Unexpected '{' in field name when doing string formatting

Unexpected '{' in field name when doing string formatting Question: I’m trying to write a small script that will automate some PHP boilerplate that I need to write. It should write a copy of the string code to the output file with the various replacement fields filled in for each dict in the fields list. …

Total answers: 1

Text formatting error: '=' alignment not allowed in string format specifier

Text formatting error: '=' alignment not allowed in string format specifier Question: What does ‘=’ alignment mean in the following error message, and why does this code cause it? >>> “{num:03}”.format(num=”1″) Traceback (most recent call last): File “<stdin>”, line 1, in <module> ValueError: ‘=’ alignment not allowed in string format specifier The code has a …

Total answers: 8

String with 'f' prefix in python-3.6

String with 'f' prefix in python-3.6 Question: I’m trying out Python 3.6. Going through new code, I stumbled upon this new syntax: f”My formatting string!” It seems we can do things like this: >>> name = “George” >>> print(f”My cool string is called {name}.”) My cool string is called George. Can anyone shed some light …

Total answers: 4

What is the difference between !r and %r in Python?

What is the difference between !r and %r in Python? Question: As the title states, what is the difference between these two flags? It seems they both convert the value to a string using repr()? Also, in this line of code: “{0!r:20}”.format(“Hello”) What does the 0 in front of the !r do? Asked By: b_pcakes …

Total answers: 1