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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove gilberishs from a "string" kucingkembar 2 147 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
  Function to count words in a list up to and including Sam Oldman45 15 6,341 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
Smile please help me remove error for string.strip() jamie_01 3 1,119 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,277 Sep-27-2022, 01:38 PM
Last Post: buran
  Remove a space between a string and variable in print sie 5 1,680 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  How do I remove spurious "." from a string? Zuhan 7 1,927 Apr-12-2022, 02:06 PM
Last Post: Pedroski55
  Including data files in a package ChrisOfBristol 4 2,440 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,352 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  Regex: a string does not starts and ends with the same character Melcu54 5 2,343 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  [solved] unexpected character after line continuation character paul18fr 4 3,270 Jun-22-2021, 03:22 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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