Python Forum
How to find previous string of a string
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find previous string of a string
#1
Hi,

I am struggling at problem which is how to find previous string in a string.

For example i have a string
A= "my name is prince"

Now i want to find "prince" when prince found return "my name is"

How can i do that?
Reply
#2
Explore string slicing. See what the below gives you (the literal string can be replaced by a variable).

"Mary had a little lamb"[:10]

So, you can find where the string you are looking for starts, and then slice the string to return everything before that.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: string indices must be integers deneme2 2 645 Feb-14-2025, 12:23 AM
Last Post: deneme2
  How do I parse the string? anna17 8 2,121 Feb-13-2025, 07:08 AM
Last Post: michaeljordan
  question about changing the string value of a list element jacksfrustration 4 2,094 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,121 Dec-19-2024, 11:57 AM
Last Post: snippsat
  [SOLVED] Sub string not found in string ? jehoshua 4 1,415 Dec-03-2024, 09:17 PM
Last Post: jehoshua
  extracting from a string Stephanos 6 1,183 Oct-01-2024, 06:52 AM
Last Post: DeaD_EyE
  Unable to understand the function string.split() Hudjefa 8 2,468 Sep-16-2024, 04:25 AM
Last Post: Pedroski55
Question [SOLVED] How to replace characters in a string? Winfried 2 1,039 Sep-04-2024, 01:41 PM
Last Post: Winfried
  Destructor method adding in string chizzy101010 3 923 Sep-03-2024, 12:31 PM
Last Post: chizzy101010
  extract an element of a list into a string alexs 5 3,660 Aug-30-2024, 09:24 PM
Last Post: alexs

Forum Jump:

User Panel Messages

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