0

Please help me with the following question:

There is a string:

Courses :- Thank You, Help me with this question, Have a good day

I would like to ignore any punctuation between "Thank You" and "Courses". what I am doing for now is:

        if "Courses" in c:
        print(c)
        idx = c.index('-')
        while not c[idx].isalpha():
            idx += 1
        old_courses = c[idx:]
        print(old_courses)      

I can get: Thank You, Help me with this question, Have a good day

But there will be any other punctuations between "Thank You" and "Courses". What can I do to get the same thing like above? Maybe can use string module.

THANK YOU!!!

Praveen
  • 8,945
  • 4
  • 31
  • 49
  • A simple way would be to loop over all characters and only select those which you want to keep, and rebuild the string from those. This should be doable with very little extra knowledge, basically only loops. It's not necessarily the most efficient way, but easy to write down. – Jan Christoph Terasa Feb 12 '21 at 15:29
  • you can try `string.split(' ')` is the structure is `Course XX some text` – Epsi95 Feb 12 '21 at 15:29
  • In this case it will be `' '.join(c.split(' ')[2:])` – Epsi95 Feb 12 '21 at 15:30

5 Answers5

0

I'd do that like this

>>> s = "Courses :- Thank you, Help me with this question"
>>> punctuations = ['.',',',':','-']
>>> newstr = [x for x in s if not x in punctuations]
>>> newstr = ''.join(newstr)
>>> newstr
'Courses  Thank you Help me with this question'

You probably want spaces and numbers included in your string. That is the reason I didn't use isalpha method. Better to do list chars you want to remove (or keep, whatever is easier).

Hopefully I understood your need

ex4
  • 2,289
  • 1
  • 14
  • 21
0

Solution using regular expression

import re
import string

s = 'Courses :- Thank You, Help me with this question, Have a good day'

re.compile(f'[{re.escape(string.punctuation)}]').sub('', s)

'Courses  Thank You Help me with this question Have a good day'
0

Try this if the format of that string stays same regardless of the symbol.

if "Courses" in c:
    new_c = ' '.join(c.split()[2:])
    print(new_c)
SURYA TEJA
  • 204
  • 3
  • 8
0

You can use the built in string.replace() function or the regular expression module.

Here are good answers for more information:

Best way to replace multiple characters in a string?

Python strip() multiple characters?

KaPy3141
  • 161
  • 14
0

You can match all the punctuation's or spaces between the words using a character class to match at least a char : or - using \s*[:-][\s:-]* and capture the words in 2 groups.

In the replacement use the 2 groups with a space in between.

import re
c = "Courses :- Thank You, Help me with this question, Have a good day"
result = re.sub(r"\b(Courses)\s*[:-][\s:-]*\b(Thank You)\b", r"\1 \2", c)
print(result)

Output

Courses Thank You, Help me with this question, Have a good day

Or with lookarounds and a single space in the replacement.

import re
c = "Courses :- Thank You, Help me with this question, Have a good day"
result = re.sub(r"(?<=\bCourses)\s*[:-][\s:-]*(?=\bThank You\b)", r" ", c)
print(result)
The fourth bird
  • 154,723
  • 16
  • 55
  • 70