Removing from a string all the characthers included between two specific characters in Python

2024/11/20 7:21:05

What's a fast way in Python to take all the characters included between two specific characters out of a string?

Answer

You can use this regular expression: \(.*?\). Demo here: https://regexr.com/3jgmd

Then you can remove the part with this code:

import re
test_string = 'This is a string (here is a text to remove), and here is a text not to remove'
new_string = re.sub(r" \(.*?\)", "", test_string)

This regular expression (regex) will look for any text (without line break) in brackets prepended by a space

https://en.xdnf.cn/q/118446.html

Related Q&A

Pyside6: Create QTabWidget with function rather than class

Ive been trying to make an application using Pyside6 and cant seem to understand why we cant create a QDialog having QTabWidget with just functions. I am not sure if I am making a mistake somewhere so,…

Pythons End Of Life

What exactly will happen to Python 2.7 after 1/2020?I understand that Python 2.7 will no longer be supported but what will actually happen? Does it mean that decision makers will delete the whole cod…

Gathering numerical data from a string input

I would like to get user input for their credit rating e.g AAA, A, BBB etc and then assign an interest rate to this. For example, if the user has a good credit rating e.g AAA I would charge an interest…

Getting Turtle in Python to recognize click events [duplicate]

This question already has an answer here:Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates(1 answer)Closed 5 months ago.Im trying to make Connect …

Delete last widget from gridlayout

I have a Grid layout in which I add Qlineedits at runtime. while pushing the button I want to delete the last qline edit from the gridlaout Why does this function delete all qlinedits at the same time …

Counting unique words

Question:Devise an algorithm and write the Python code to count the number of unique words in a given passage. The paragraph may contain words with special characters such as !, ?, ., , , : and ; and …

Django cant find template dir?

Originally I had just one app in my Django project, the templates consisted of an index.html a detail.html and a layout.html ... the index and detail files extended the layout. They all lived in the sa…

Python Programming Loop

Im doing an assignment where I have to conduct a quiz for different topics. This is my code so far.print("Hello and welcome to Shahaads quiz!") #Introduction name = input("What is your n…

How to fix stale element error without refreshing the page

Trying to get details of Tyres on this page. https://eurawheels.com/fr/catalogue/INFINY-INDIVIDUAL . Each tyre has different FINITIONS. The price and other details are different for each FINITIONS. I w…

Fastest way in numpy to get distance of product of n pairs in array

I have N number of points, for example: A = [2, 3] B = [3, 4] C = [3, 3] . . .And theyre in an array like so: arr = np.array([[2, 3], [3, 4], [3, 3]])I need as output all pairwise distances in BFS (Bre…