-2

suppose, I have a string, s="panpanIpanAMpanJOEpan" . From this I want to find the word pan and replace it with spaces so that I can get the output string as "I AM JOE". How can I do it??

Actually I also don't know how to find certain substring from a long string without spaces such as mentioned above. It will be great if someone helps me learning about this.

mas883
  • 17
  • 4
  • 1
    Try: `s.replace('pan', ' ')` [replace](https://www.tutorialspoint.com/python/string_replace.htm) finds all occurrences of a substring and replaces with a different substring. – DarrylG Dec 08 '20 at 11:37
  • 1
    Does this answer your question? [How to use string.replace() in python 3.x](https://stackoverflow.com/questions/9452108/how-to-use-string-replace-in-python-3-x) – Nick Dec 08 '20 at 11:38

4 Answers4

1

If you don't know pan you can exploit that the letters you want to find is all upper case.

fillword =  min(set("".join(i if i.islower() else ' ' for i in s).split(' '))-set(['']),key=len)

This works by first replacing all upper case letters with space, then splitting on space and finding the minimal nonempty word.

Use replace to replace with space, and then strip to remove excess spacing.

 s="panpanIpanAMpanJOEpan"
 s.replace(fillword,' ').strip()

gives:

'I AM JOE'
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28
0
s="panpanIpanAMpanJOEpan"
print(s.replace("pan"," ").strip())

use replace

Output:

I AM JOE 
sourab maity
  • 1,025
  • 2
  • 8
  • 16
0

As DarrylG and others mentioned, .replace will do what you asked for, where you define what you want to replace ("pan") and what you want to replace it with (" ").

To find a certain string in a longer string you can use .find(), which takes a string you are looking for and optionally where to start and stop looking for it (as integers) as arguments.

If you wanted to find all of the occurrences of a string in a bigger string there's two options:

  • Find the string with find(), then cut the string so it no longer contains your searchterm and repeat this until the .find() method returns -1(that means the searchterm is not found in the string anymore)
  • or use the regex module and use the .finditer method to find all occurences of your string Link to someone explaining exactly that on stackoverflow.

Edit: If you don't know what you are searching for, it becomes a bit more tricky, but you can write a regex expession that would extract this data as well using the same regex module. This is easy if you know what the end result is supposed to be (I AM JOE in your case). If you don't it becomes more complicated and we would need additional information to help with this.

HilbertB
  • 352
  • 1
  • 2
  • 12
0

You can use replace, to replace all occurances of a substring at once.
In case you want to find the substrings yourself, you can do it manually:

s = "panpanIpanAMpanJOEpan"

while True:
    panPosition = s.find('pan')  # -1 == 'pan' not found!
    if panPosition == -1:
        s = s.strip()
        break
    #  Cut out pan from s and replace it with a blanc.
    s = s[:panPosition] + ' ' + s[panPosition + 3:]
print(s)

Out:

I AM JOE
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47