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

  • SEARCH
  • Home
  • 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 8968053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:23:02+00:00 2026-06-15T17:23:02+00:00

>>> def mod2(n): … print ‘the remainder is’, n % 2 … >>> mod2(5)

  • 0
>>> def mod2(n):
...   print 'the remainder is', n % 2
... 
>>> mod2(5)
the remainder is 1
>>> mod2(2)
the remainder is 0
>>> mod2('%d')
the remainder is 2
>>> mod2('%d\rHELLO. I AM A POTATO!')
HELLO. I AM A POTATO!

Is there anyway to disable % symbol (operator.mod) from doing wacky string substitution stuff? I always use str.format if I need anything like that, and would generally rather this string substitution feature didn’t exist at all, giving a TypeError instead.

  • 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-15T17:23:03+00:00Added an answer on June 15, 2026 at 5:23 pm

    You can’t disable it with a switch, no. The str() type implements a __mod__ method to handle the formatting, it’s not that Python special-cased the expression just for strings.

    As such, to prevent this you either need to cast the n argument to something that is not a string (by converting it to int() for example), or subclass str() to override the __mod__ method:

    >>> class noformattingstr(str):
    ...     def __mod__(self, other):
    ...         raise TypeError('String formatting using "%" has been deprecated')
    ... 
    >>> noformattingstr('hello world: %d') % 10
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in __mod__
    TypeError: String formatting using "%" has been deprecated
    

    You can assign this to __builtins__.str, but this does not mean that all string literals will then use your subclass. You’d have to explicitly cast your str() values to noformattingstr() instances instead.

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

Sidebar

Related Questions

def download_if_dne(href, filename): if os.path.isfile(filename): # print 'already downloaded:', href return False else: if
def myFunc( a, b ): def innerFunc( c ): print c innerFunc( 2 )
def myFunc( str ): print str=, str if str == None: print str is
def dispcar ( self, reg ): print (The car information for '%s' is: ),
def checkCache(cachedText): for line in open(cache): if cachedText + : in line: print line
mod1.py import mod2 class Universe: def __init__(self): pass def answer(self): return 42 u =
def ask(): global name, loca print What's your name? name = raw_input('> ') print
def display_home(request): from datetime import * now=datetime.today() print 'Month is %s'%now.month events=Event.objects.filter(e_date__year=datetime.today().year).filter(e_date__month=datetime.today().month,e_status=1).values('e_name','e_date') return render_to_response("SecureVirtualElection/home.html",{'events':
def main(args: Array[String]) { foo(hello) } def foo(args:Any*){ bar(args) } def bar(args:Any *){ println(args)
def withPositionalArgs(*args): print args, type(args) def withTupleAsArgument(tupleArg): print tupleArg, type(tupleArg) a=1 b=2 c=[10,20] print

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.