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


Messages In This Thread
Remove from end of string up to and including some character - by lbtdne - May-17-2020, 08:31 AM

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