Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8634901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:50:13+00:00 2026-06-12T09:50:13+00:00

In Python, I’m trying to implement a pseudo-ternary operator within a template string. A

  • 0

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.sub(), you can pass a function to be called on matches. What I can’t do is to pass **kwargs to it. Code follows

import re

template_string = "some text (pseudo_test?val_if_true:val_if_false) some text"

def process_pseudo_ternary(match, **kwargs):
    if match.groups()[0] in kwargs:
        return match.groups()[1]
    else:
        return match.groups()[2]

def process_template(ts, **kwargs):
    m = re.compile('\((.*)\?(.*):(.*)\)')
    return m.sub(process_pseudo_ternary, ts)

print process_template(template_string, **{'pseudo_test':'yes-whatever', 'other_value':42})

line if match.groups()[0] in kwargs: is the problem of course, as process_pseudo_ternary’s kwargs are empty.

Any ideas on how to pass these? m.sub(function, string) doesn’t take arguments.

The final string is to be: some text val_if_true some text (because the dictionary has the key named ‘pseudo_test’).

Feel free to redirect me to a different implementation of ternary operator in a string. I’m aware of Python conditional string formatting . I need the ternary to be in the string, not in the string’s formatting tuple/dict.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:50:14+00:00Added an answer on June 12, 2026 at 9:50 am

    If i understand it correctly, you could use something like http://docs.python.org/library/functools.html#functools.partial

    return m.sub(partial(process_pseudo_ternary, custom_1=True, custom_2=True), ts)
    

    EDIT: Changed a little, to match your code better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python has re.escape() if you want to match an arbitrary string literal. In my
Python has string.startswith() and string.endswith() functions which are pretty useful. What NSString methods can
Python has a json library where you can do json.loads(s) to load json into
Python beginner here. I have a text file that is sorted into columns: fields
Python 2.6 and beyond has the ability to directly execute a .zip file if
Python has the idea of metaclasses that, if I understand correctly, allow you to
Python newb...beware! I am trying to recursively ftp some files. In the script below,
Python sorts by byte value by default, which means é comes after z and
Python regular expression I have a string that contains keywords but sometimes the keywords

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.