0

I am trying to split a string but it should be replaced to another string and return as a list. Its hard to explain so here is an example:

I have string in variable a:

a = "Hello World!"

I want a list such that:

a.split("Hello").replace("Hey") == ["Hey"," World!"]

It means I want to split a string and write another string to that splited element in the list. SO if a is

a = "Hello World! Hello Everybody"

and I use something like a.split("Hello").replace("Hey") , then the output should be:

a = ["Hey"," World! ","Hey"," Everybody"]

How can I achieve this?

5
  • 2
    a = a.replace('Hello','Hey').split('Hey') would work. Commented Sep 4, 2018 at 3:28
  • This link here to what you do. stackoverflow.com/questions/2582138/… Commented Sep 4, 2018 at 3:30
  • @EdwardKotarski Its not working with HelloWorld!. I don't have spaces in my original data. These are just examples. Commented Sep 4, 2018 at 3:35
  • @WongSiwei I don't want loops etc to make my program complex. Commented Sep 4, 2018 at 3:36
  • You should be more specific where you want to separate the string then. Do you want to separate by space or by the first uppercase character? Commented Sep 4, 2018 at 4:27

5 Answers 5

1

From your examples it sounds a lot like you want to replace all occurrences of Hello with Hey and then split on spaces.

What you are currently doing can't work, because replace needs two arguments and it's a method of strings, not lists. When you split your string, you get a list.

>>> a = "Hello World!"
>>> a = a.replace("Hello", "Hey")
>>> a
'Hey World!'
>>> a.split(" ")
['Hey', 'World!']
Sign up to request clarification or add additional context in comments.

Comments

0
x = "HelloWorldHelloYou!" 
y = x.replace("Hello", "\nHey\n").lstrip("\n").split("\n") 
print(y) # ['Hey', 'World', 'Hey', 'You!']

This is a rather brute-force approach, you can replace \n with any character you're not expecting to find in your string (or even something like XXXXX). The lstrip is to remove \n if your string starts with Hello.

Alternatively, there's regex :)

1 Comment

why would someone downvote this answer? It seems to be the best so far. well a bit of modification. try doing x.replace("Hello", "\nHey\n").strip().split() instead. This will split in spaces and line breaks too. Also it will do both stripping on both sides
0

this functions can do it

def replace_split(s, old, new):
    return sum([[blk, new for blk] in s.split(old)], [])[:-1]

1 Comment

Downvoted because this is extremely overcomplicated and doesn't answer the question directly.
0

It wasnt clear if you wanted to split by space or by uppercase.

import re    

#Replace all 'Hello' with 'Hey'

a = 'HelloWorldHelloEverybody'
a = a.replace('Hello', 'Hey')

#This will separate the string by uppercase character

re.findall('[A-Z][^A-Z]*', a) #['Hey', 'World' ,'Hey' ,'Everybody']

Comments

0

You can do this with iteration:

a=a.split(' ')
for word in a:
    if word=='Hello':
      a[a.index(word)]='Hey'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.