How to subtract multiple string values from another string [duplicate] - python

This question already has answers here:
Replacing specific words in a string (Python)
(4 answers)
Closed 6 years ago.
With:
abc = 'abc'
xyz = 'xyz'
word = 'begin abc- middle_xyz_ end'
I need to extract the values of abc and xyz from word.
The result should be
result = 'begin - middle__ end'
How to achieve this with a minimum amount of code?

You use replace() with an empty string as the value to replace with.
result = word.replace('abc','').replace('xyz','')

Related

How to delete certain words from string without spaces? [duplicate]

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')

How to get a string after and before a specific substring in Python? [duplicate]

This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 2 years ago.
How can I get a string after and before a specific substring?
For example, I want to get the strings before and after : in
my_string="str1:str2"
(which in this case it is: str1 and str2).
Depending on your use case you may want different things, but this might work best for you:
lst = my_string.split(":")
Then, lst will be: ['str1', 'str2']
You can also find the index of the substring by doing something like:
substring = ":"
index = my_string.find(":")
Then split the string on that index:
first_string = my_string[:index]
second_string = my_string[index+len(substring):]

Using regular expression to count the number of spaces at the beginning of a string [duplicate]

This question already has answers here:
Check string indentation?
(4 answers)
Closed 4 years ago.
How can I use regex to count the number of spaces beginning of the string. For example:
string = ' area border router'
count_space variable would return me a value of 1 since there is 1 whitespace at the beginning of the string. If my string is:
string = ' router ospf 1'
count_space variable would return me a value of 2 since there is 2 whitespace at the beginning of the string. And so on....
I thing the expression would be something like RE = '^\s' ? But not sure how to formulate it.
You don't need regex, you can just do this:
s = ' area border router'
print(len(s)-len(s.lstrip()))
Output:
1

How to extract 4 characters after '-' in a hostname [duplicate]

This question already has answers here:
How to get a string after a specific substring?
(9 answers)
Closed 7 years ago.
I have hostname of format xxxxxxxx-abcdxxxxx the x is not a set number so can't use print text[10:14] because I don't have a set location, the only pattern is 4 chars after -.
Assuming your first string is
s = "xxxxxxxx-abcdxxxxxxxxx"
you just do:
s.split("-",1)[1][:4]
which splits s into two strings in an array, ['xxxxxxxx','abcdxxxxxxxxx'] and you get the result by taking the splicing of the 2nd array from index 0 to 4.
abcd
Option 1
Get the index of the dash and select from +1 to +5:
a = 'xxxxxxx-abcdxxxxxxx'
i = a.index('-')
print(i[i+1:i+5])
Option 2
Use the split function and then get the first 4 values of the second element.
a = 'xxxxxxx-abcdxxxxxx'
print(a.split('-')[1][:4])
To see if a string is alphabetic, simply call the isalpha function:
str.isalpha()
It will return true or false based on result.

Get the last 4 characters of a string [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 7 years ago.
I have the following string: "aaaabbbb"
How can I get the last four characters and store them in a string using Python?
Like this:
>>> mystr = "abcdefghijkl"
>>> mystr[-4:]
'ijkl'
This slices the string's last 4 characters. The -4 starts the range from the string's end. A modified expression with [:-4] removes the same 4 characters from the end of the string:
>>> mystr[:-4]
'abcdefgh'
For more information on slicing see this Stack Overflow answer.
str = "aaaaabbbb"
newstr = str[-4:]
See : http://codepad.org/S3zjnKoD

Categories