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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:18:40+00:00 2026-06-09T06:18:40+00:00

I am brand new to Python and have been working with it for a

  • 0

I am brand new to Python and have been working with it for a few weeks. I have a list of strings and want to remove the first four and last four characters of each string. OR, alternatively, removing specific character patterns (not just specific characters).

I have been looking through the archives here but don’t seem to find a question that matches this one. Most of the solutions I have found are better suited to removing specific characters.

Here’s the strings list I’m working with:

sites=['www.hattrick.com', 'www.google.com', 'www.wampum.net', 'www.newcom.com']

What I am trying to do is to isolate the domain names and get

[hattrick, google, wampum, newcom]

This question is NOT about isolating domain names from URLs (I have seen the questions about that), but rather about editing specific characters in strings in lists based upon location or pattern.

So far, I’ve tried .split, .translate, .strip but these don’t seem appropriate for what I am trying to do because they either remove too many characters that match the search, aren’t good for recognizing a specific pattern/grouping of characters, or cannot work with the location of characters within a string.

Any questions and suggestions are greatly appreciated, and I apologize if I’m asking this question the wrong way etc.

  • 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-09T06:18:43+00:00Added an answer on June 9, 2026 at 6:18 am
    def remove_cruft(s):
        return s[4:-4]
    
    sites=['www.hattrick.com', 'www.google.com', 'www.wampum.net', 'www.newcom.com']
    [remove_cruft(s) for s in sites]
    

    result:

    ['hattrick', 'google', 'wampum', 'newcom']
    

    If you know all of the strings you want to strip out, you can use replace to get rid of them. This is useful if you’re not sure that all of your URLs will start with “www.”, or if the TLD isn’t three characters long.

    def remove_bad_substrings(s):
        badSubstrings = ["www.", ".com", ".net", ".museum"]
        for badSubstring in badSubstrings:
            s = s.replace(badSubstring, "")
        return s
    
    sites=['www.hattrick.com', 'www.google.com', 
    'www.wampum.net', 'www.newcom.com', 'smithsonian.museum']
    [remove_bad_substrings(s) for s in sites]
    

    result:

    ['hattrick', 'google', 'wampum', 'newcom', 'smithsonian']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am brand new to Python, and have been trying to get the Django
Brand new to the Log4Net library and there is something I have not been
I'm brand new to Google App Engine and have just been playing around with
Python 2.5.4. Fairly new to Python, brand new to decorators as of last night.
First I'm brand new to JS but have an idea that object classes are
Brand new to coding; been utilizing stackoverflow religiously for months; first time asking a
I'm brand new to python, but I have a lot of experience with C++,
I'm brand new to Apache Ivy and have posted a few other Ivy-related questions
I'm brand new to working with the google maps api, and i've been stuck
Brand new to python, Let's say I have a dict: kidshair = {'allkids':{'child1':{'hair':'blonde'}, 'child2':{'hair':'black'},

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.