This question already has answers here:
Python strip unexpected behavior
(2 answers)
Closed 4 years ago.
I would like to strip the ending from a string, as in following:
da = "abc.com"
print(da.strip(".com"))
My expected outcome is abc. However, ab is returned instead.
Why is that and how to solve it?
this should work: simply split string by . and discard the last piece.
print(da.split('.')[:-1])
Using Regex.
import re
da = "abc.com"
print(re.sub("\.com", "", da))
or
print(da.replace(".com", ""))
Output:
abc
Related
This question already has answers here:
Python split() without removing the delimiter [duplicate]
(4 answers)
Closed 11 months ago.
Is it possible to separate the string "a!b!" into two strings "a!" and "b!" and store that in a list? I have tried the split() function (and even with the delimiter "!"), but it doesn't seem to give me the right result that I want. Also, the character "!" could be any character.
How about :
string = 'a!ab!b!'
deliminator = '!'
word_list = [section+deliminator for section in string.split(deliminator) if section]
print(word_list)
Output :
['a!', 'ab!', 'b!']
split() is used when you need to seperate a string with particular character. If you want split a string into half, Try this
s = "a!b!"
l = [s[ : len(s)//2], s[len(s)//2 : ]]
# output : ["a!", "b!"]
This question already has answers here:
Python extract pattern matches
(10 answers)
Closed 1 year ago.
I made this code:
import re
match = re.search(r'[DER]\d+[Y]', 'DER1234Y' )
print(match.group())
and it prints this :
R1234Y
I want the code to only print the numbers and nothing else. How to do that ?
It's basically regex. So would this work?: re.sub('[^0-9]+', '', 'DER1234Y')
[^0-9]+ = everything that is not a numeric value (0-9).
This question already has answers here:
How to remove substring from string in Python 3
(2 answers)
Closed 2 years ago.
Is there I way to delete words from a string in Python if it doesn't have spaces. For example, if you have the string "WUBHELLOWUB" I want to remove "WUB". I tried
s = 'WUBHELLOWUB'
while 'WUB' in s:
ind = s.find('WUB')
s = s[:ind] + s[ind+1:]
print(s)
but it did not work.
You can use regex
import re
data=r"\S*WUB\S*"
re.sub(data, '','WUBWUBHELLO')
This question already has answers here:
How to replace multiple substrings of a string?
(28 answers)
Closed 5 years ago.
I am working with Python 3 and I want to replace the emoticons included in a dictionary.
For example
text = "Hi, I'm coming home :)"
#Create dictionary
dict_lookup = {':(' : 'sad',
':)' : 'happy'}
The desired output is:
Hi, I'm coming home happy
What is the most efficient way to achieve this result in Python 3?
This should do the trick:
for emote, replacement in dict_lookup.items():
text = text.replace(emote, replacement)
Take a look at str.replace
It allows you to do text.replace(dict_key, dict_value)
This question already has answers here:
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
(7 answers)
Closed 6 years ago.
Simple, simple question, hope you can help me:
How do I add a string to a regex?
Say:
d = '\d\d\d'
mo = re.compile(r #d goes here)
Pasting it, separating it with a comma, or with a plus gives me errors.
Normally, as you know, it would be re.compile(r'\d\d\d')
Is this what you are looking for?
d = r"\d\d\d"
re.compile(d)
Maybe more intuitive:
d = r"\d{3}"
# match a digit exactly three times, consecutively
re.compile(d)