Is there a way to cut strings based on multiple characters?

Asked 1 years ago, Updated 1 years ago, 75 views

No matter how much I look for it, I only see cutting based on blank characters and removing blank characters from the front and back I can't cut strings based on multiple characters.

mystr = Hey, you - what are you doing here!? -> ['hey', 'you', 'what', 'are', 'you', 'doing', 'here'] Blank + special character , like -?!I'd like to distribute all strings based on .

But Python str.split() only gets one factor. What should I do?

string python split

2022-09-22 22:28

1 Answers

import re
DATA = "Hey, you - what are you doing here!?"
print re.findall(r"[\w']+", DATA)
# # Prints ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here']
import re
DATA = "Hey, you - what are you doing here!?"
print re.split('\W+', DATA) # ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here', '']

'a;bcd,ef g'.replace(';',' ').replace(',',' ').split() = ['a', 'bcd', 'ef', 'g']

You do not need to import modules The disadvantage is that replace() accepts only one factor and requires multiple calls.


2022-09-22 22:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.