Python Forum
Remove from end of string up to and including some character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove from end of string up to and including some character
#1
Hi all,

I'm trying to take some string, stri_ , and iterate through it until I find a particular character. Then, I will update the stri_ to be stri_ with that character and everything after it removed.

#  Working with url_ , a string of any length
url_ = url_[:url_.find(".")]

#  For url_ == "wikipedia.com", url_ should become "wikipedia".
print(url_)  # wikipedia
The above works fine in most cases, however I ran into problems with the below.

#  Working with url_ , a string of any length
url_ = url_[:url_.find(".")]

#  For url_ == "en.wikipedia.com", url_ should become "en.wikipedia".
print(url_)  # en
Is there any way I can do something similar to rstrip(), but instead of removing a single character, remove every character rstrip() checked before it found "." ie. the last "." in the string?

Thanks,

K
Reply
#2
You were on the right lines with rstrip. Instead use rsplit(), which takes two arguments. A delimiter and a maxsplit.

result = url_.rsplit('.', 1)[0]
It doesn't matter how many '.' you have at the front of the string, it will only every remove anything after the last '.':
Output:
>> a = "wikipedia.com" >> print(a.rsplit(".", 1)[0]) >> wikipedia >> a = "en.wkikipedia.com" >> print(a.rsplit(".", 1)[0]) >> en.wkikipedia >> a = "en.something.somethingelse.wikipedia.com" >> print(a.rsplit(".", 1)[0]) >> en.something.somethingelse.wikipedia
Reply
#3
change url_find('.') to url_rfind('.') will give you what you want
Output:
>>> url = 'wikipedia.com' >>> print(url[:url.rfind('.')]) wikipedia >>> url = 'en.wikipedia.com' >>> print(url[:url.rfind('.')]) en.wikipedia
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove char from string?? ridgerunnersjw 2 159 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  Including modules in Python using sys.path.append JoeDainton123 1 342 Aug-24-2020, 04:51 AM
Last Post: millpond
  Including a Variable In the HTML Tags When Sending An Email JoeDainton123 0 227 Aug-08-2020, 03:11 AM
Last Post: JoeDainton123
  SyntaxError: unexpected character after line continuation character siteshkumar 2 350 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Remove escape characters / Unicode characters from string DreamingInsanity 5 960 May-15-2020, 01:37 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 589 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Highlight and remove specific string of text itsalmade 5 725 Dec-11-2019, 11:58 PM
Last Post: micseydel
  How to get the index of a character from a string chihaya 1 500 Dec-03-2019, 12:54 PM
Last Post: buran
  "Up to but not including" (My personal guide on slicing & indexing) Drone4four 5 602 Nov-20-2019, 09:38 PM
Last Post: newbieAuggie2019
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 2,289 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020