Linked Questions

8109 votes
32 answers
3.1m views

Is there a ternary conditional operator in Python?
Devoted's user avatar
  • 185k
0 votes
2 answers
62 views

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 ...
Kevin Yao's user avatar
  • 139
612 votes
15 answers
1.5m views

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 ...
Ricky Robinson's user avatar
65 votes
7 answers
59k views

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 ...
mhlester's user avatar
  • 23.3k
23 votes
7 answers
28k views

'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 ...
user avatar
22 votes
4 answers
41k views

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 ...
jmd_dk's user avatar
  • 13.2k
-2 votes
2 answers
5k views

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}'....
matteo's user avatar
  • 5,001
1 vote
4 answers
2k views

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 ...
Fabio's user avatar
  • 2,130
2 votes
2 answers
321 views

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;' ...
tvo's user avatar
  • 789
2 votes
2 answers
380 views

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 ...
benshepherd's user avatar
1 vote
1 answer
384 views

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....
user avatar
0 votes
4 answers
63 views

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 ...
Sakeer's user avatar
  • 1,998