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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:55:58+00:00 2026-05-16T21:55:58+00:00

How can I replace the contents of strings with # ‘s in Python? Assume

  • 0

How can I replace the contents of strings with #‘s in Python? Assume no comments, no multiple lines for one string. Like if there is a line in a python file:

print 'Hello' + "her mom's shirt".

This will be translated into:

print '#####' + "###############".

It’s like a filter to deal with every line in a python file.

  • 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-05-16T21:55:58+00:00Added an answer on May 16, 2026 at 9:55 pm
    >>> import re
    >>> s="The Strings"
    >>> s=re.sub("\w","#",s)
    >>> s
    '### #######'
    >>> s='Hello' + "her mom's shirt"
    >>> s
    "Helloher mom's shirt"
    >>> re.sub("\w","#",s)
    "######## ###'# #####"
    

    —-Edit

    OK, Now I understand that you want the output to be from a Python file. Try:

    import fileinput
    import re 
    
    for line in fileinput.input():
        iter = re.finditer(r'(\'[^\']+\'|"[^"]+")',line)
        for m in iter:
           span = m.span()
           paren = m.group()[0]
           line = line[:span[0]]+paren+'#'*(span[1]-span[0]-2)+paren+line[span[1]:] 
           print line.rstrip()
    

    This does not deal with line breaks, the “”” form, and is only tested again 1 or two files I have…

    In general, it is better to use a parser for this kind of job.

    Best

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

Sidebar

Related Questions

I have the following formula: =ArrayFormula(SUM(Morten!$M4:$M400)) How can I replace Morten with the contents
so 1GvG:s/..../g can replace over an entire buffer However, suppose I have multiple vim
I need to know how I can replace the last s from a string
I was wondering if there is any way that I can replace substrings within
There are several occurences of ab:tag_x tag in a string. Tag format: <ab:tag_x contents=some
My current regex is /[$]([a-zA-Z0-9_\-\,:]*)[$]/ and this will allow me to replace strings like
Given a string with replacement keys in it, how can I most efficiently replace
I've got multiple commands in a command line that builds a text string in
I'm trying to replace the contents inside braces on a string with preg_replace_callback .
How can replace values as array by replace in javascript. I want done replace

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.