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
#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


Messages In This Thread
RE: Remove from end of string up to and including some character - by DreamingInsanity - May-17-2020, 09:03 AM

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