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
  Spaces in string Mallard 9 473 Dec-19-2025, 01:04 PM
Last Post: Mallard
  Convert any Python expression to a string voidtrance 2 1,077 Jun-23-2025, 07:06 AM
Last Post: DeaD_EyE
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,464 May-28-2025, 07:56 AM
Last Post: Winfried
  Return a string or byte object from Enum class? Calab 5 1,852 May-14-2025, 05:21 PM
Last Post: snippsat
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 1,612 May-01-2025, 03:43 PM
Last Post: Winfried
  Get the string after a specific char JanJan 5 1,714 Apr-30-2025, 05:04 AM
Last Post: snl_9527
  TypeError: string indices must be integers deneme2 2 1,368 Feb-14-2025, 12:23 AM
Last Post: deneme2
  How do I parse the string? anna17 8 3,235 Feb-13-2025, 07:08 AM
Last Post: michaeljordan
  question about changing the string value of a list element jacksfrustration 4 3,078 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 2,846 Dec-19-2024, 11:57 AM
Last Post: snippsat

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.