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 8269695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:18:54+00:00 2026-06-08T06:18:54+00:00

In my program, the user enters a term which I process before sending on.

  • 0

In my program, the user enters a term which I process before sending on. Part of this process is to change all instances of ‘and’,’or’ and ‘not’ to uppercase letters but leaving the rest intact.

I can’t use string.upper() because it changes everything to uppercase; or string.replace() because if ‘and’ is in another word in the string e.g. ‘salamander’ it will also change that to ‘salamANDer’. I think my best option is the regex re.sub() function. This allows me to change full words which is perfect. Next problem: I have to do a re.sub() function for each change I want to make. Is it possible to make one statement to do all the changes? What I have done isn’t wrong but I don’t think its necessarily good practice:

>>import urllib2
>>import re
>>query = 'Lizards and Amphibians not salamander or newt'
>>query=re.sub(r'\bnot\b', 'NOT',query)
>>query=re.sub(r'\bor\b', 'OR',query)
>>query=re.sub(r'\band\b', 'AND',query)
>>query = urllib2.quote("'"+query+"'")

>>print query
%27Lizards%20AND%20Amphibians%20NOT%20salamander%20OR%20newt%27
  • 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-08T06:18:56+00:00Added an answer on June 8, 2026 at 6:18 am

    You can pass a function substitution expression in re.sub():

    >>> term = "Lizards and Amphibians not salamander or newt"
    >>> re.sub(r"\b(not|or|and)\b", lambda m: m.group().upper(), term)
    'Lizards AND Amphibians NOT salamander OR newt'
    

    However, I’d probably go with a non-regex solution:

    >>> " ".join(s.upper() if s.lower() in ["and", "or", "not"] else s
    ...          for s in term.split())
    'Lizards AND Amphibians NOT salamander OR newt'
    

    This also normalizes the whitespace and works with mixed-case words like And.

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

Sidebar

Related Questions

I'm trying to build a program that goes like this: User enters 3 numbers.
I have a program in which the user enters a username and a date
I have written a small program in which a user enters minutes and program
My program takes user name and password authentication from user before initialising the program,
When a user enters a whole number, the program runs smoothly, but when the
In my program a user enters a url string, say http://www.engineering.uiowa.edu/~hawkeng//fall01/graphics/potato.gif how would I
I am trying to work on a program where the user enters a random
I am trying to program a converter in iOS. The user enters values into
The user enters 3 numbers and I'm assuming all are proper integers and distinct.
I have a program in which the user inputs information into two separate text

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.