Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I need help, in a long string, I want to know the the 2 characters after a number
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
if in example after 1 number == "gr":
so delete "gr"
If I understand your question correctly, what you need is regex:
import re
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
re.sub("(\d)gr", r"\1", example)
Output
gz15123da1az41sd23f168zgre4r6z
Explnatation
The re.sub function takes three arguments: the first argument is a patter, the second one the replacement, and the last of is the string itself. (\d)gr selects the parts that contain a number followed by gr, then replaces it with the number (\d) itself(removing the gr part).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
How can I separate data types in a string or list so they can be set to another character, I assume there is something I have missed but everything i have tried so far has now worked for me. so far have tried so split into list and use a for loop to find every int but I can't find a way to differentiate the data types so it can change every int.
You can use regex :
import re
re.sub("\d", "_", "10 4 2")
\d matches any decimal digit character.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Id codes at a company come in the form x-y-zzzzzz, where x is a digit and y is a letter and zzzzzz represents a string of 6 letters. Write a function which takes in a code as an input (e.g. 3-a-abaabb) and returns the zzzzzz part (e.g. abaabb).
I have no idea how to start and solve this question. any help would be much appreciated. My IDE is pycharm (solving python coding problems) I basically need to create a function which takes the code as an input and will return the last 6 letters
You can use str.spit('-') then search count in code with repeat is equal or not, like below:
def fnd_code(code):
repeat, char, search = code.split('-')
return search.count(char) == int(repeat)
print(fnd_code('3-a-abaabb'))
print(fnd_code('4-a-abaabb'))
Output:
True
False
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want the data enclosed between three asterisks.And the Word should start with description.
For eg:I have data like
description ***tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568***;
I want only
tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568
You may use re.findall here:
inp = "description ***tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568***;"
matches = re.findall(r'\bdescription\s+\*{3}(.*?)\*{3}', inp, flags=re.DOTALL)
print(matches)
This prints:
['tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568']
Note that I use dot all mode in the regex, in case your expected matches might span across more than one line.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The string i needed to format is,
string = "How are you? abcdef"
I need to remove the "abcdef" from the string.
string = string.split(' ')[0]
Edit: Explanation. The line of code above will split the single string into a list of strings wherever there is a double space. It is important to note that whatever is split upon, will be removed. [0] then retrieves the first element in this newly formed list.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a program that prints out some data with 'p's in place of decimal points, and also some other information. I was trying to replace the 'p's with '.'s.
Example output by the program:
out_info = 'value is approximately 34p55'
I would like to change it to:
out_info_updated = 'value is approximately 34.55'
I tried using re.search to extract out the number and replace the p with ., but plugging it back becomes a problem.I could not figure out that pattern to use for re.sub that would do the job.
Can anyone please help?
Here you go:
import re
out_info = "value is approximately 34p55"
re.sub(r'(\d+)p(\d+)', r'\1.\2', out_info)
The output is:
'value is approximately 34.55'
That says "Look for one or more digits, followed by a p, followed by one or more digits, then replace all that with the first set of digits, followed by a ., followed by the second set of digits."