Linked Questions
12 questions linked to/from How to implement conditional string formatting?
8109 votes
32 answers
3.1m views
Does Python have a ternary conditional operator?
Is there a ternary conditional operator in Python?
0 votes
2 answers
62 views
When rounding integer, don't add to the end [duplicate]
I currently am trying to work with a number that has variable decimal place lengths. It can either be an integer, or have up to 10 decimals i.e. 33.3333333. I wanted to restrict the number to only ...
612 votes
15 answers
1.5m views
How to write inline if statement for print?
I need to print some stuff only when a boolean variable is set to True. So, after looking at this, I tried with a simple example: >>> a = 100 >>> b = True >>> print a if b ...
65 votes
7 answers
59k views
Plural String Formatting
Given a dictionary of ints, I'm trying to format a string with each number, and a pluralization of the item. Sample input dict: data = {'tree': 1, 'bush': 2, 'flower': 3, 'cactus': 0} Sample output ...
23 votes
7 answers
28k views
Python inline elif possible?
'Hello ' + ('there' if name is None else name) Is the equivalent of msg = 'Hello ' if name is None: msg += 'there' else: msg += name What is the equivalent of this: msg = 'Hello ' if name ...
22 votes
4 answers
41k views
Why isn't it possible to use backslashes inside the braces of f-strings? How can I work around the problem? [duplicate]
In Python >=3.6, f-strings can be used as a replacement for the str.format method. As a simple example, these are equivalent: '{} {}'.format(2+2, "hey") f'{2+2} {"hey"}' Disregarding format ...
-2 votes
2 answers
5k views
Conditional string formatting with if, elif
one step further than How to implement conditional string formatting? Basically: is it possible to add if, elif.... else in a format string? l = ['it', 'en', 'es'] for i in l: print('{tit}'....
1 vote
4 answers
2k views
conditional display of string in Python
I have a program for enumerating the users with administrative privileges on Windows. I also want to display the number of accounts found which is stored in a variable called num_administrator. I have ...
2 votes
2 answers
321 views
How to format a string in Python that when executed produces a valid if .. then .. else statement?
I want to write a Python string that when executed does this: if condition: consequence else: alternative So, I tried something like: string = 'if condition: consequence; else: alternative;' ...
2 votes
2 answers
380 views
Format string with possible non-existing arguments in Python
I've searched for this, but I'm not sure how to word what I'm asking, so it's difficult to find whether somebody has already posted a solution. I'd like to format some variables into a descriptive ...
1 vote
1 answer
384 views
pass kwargs to re.sub()
In Python, I'm trying to implement a pseudo-ternary operator within a template string. A value is inserted into a string if kwargs has a specific key. re module has a way do exactly what I need in re....
0 votes
4 answers
63 views
format string based on conditions
I am trying to create a mail body template dynamically based on some conditions. body = """ New product has created by {user} This can be viewed by clicking here {link} Logs is ...