Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to append parenthesis to the string text in python?

I have one string like "Article created on March 03 2021 for the some user"

I want to convert that string to "Article created on (March 03 2021 for the some user)" so I thought I should create a list containing all the name of the months, and then scan the string and if any month found then it should add starting parenthesis "(" to the left of the month’s name and another closing parenthesis ")" at the end of the string.

How can I achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We can try using re.sub here:

inp = "Article created on March 03 2021 for the some user"
output = re.sub(r'(\w+ \d{2} \d{4}\b.*)', r'(\1)', inp)
print(output)  # Article created on (March 03 2021 for the some user)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading