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.
Related
This question already has answers here:
Python - Regular expressions get numbers between parenthesis
(2 answers)
Closed 2 years ago.
I have a column with values like this:
4 (3 in force)
44 (39 in force)
I was able to use this to get a new column for the first number.
df['new'] = df['column'].str.extract('(\d+)')
How can I get a new column for the second number? (3,39, etc.)
One way that specifically answers you question would be to use a lookbehind regular expression, that basically says "the first number after another number, a space and a parenthesis":
df['new'] = df['column'].str.extract('(?<=\d+\s\()\d+')
But if you're extracting multiple parts from a single string, you might consider combining the two and using groups in the regex to access the parts you want.
You could just take the row, convert it into a string, split it, and access the needed numbers, for example:
row = '4 (3 in force)'
row.split(' ') # This returns ['4', '(3', 'in', 'force)']
row.split(' ')[1] # This returns '(3'
row.split(' ')[1][1:] # And this returns all numbers after the bracket, so '3'
This question already has answers here:
Why are str.count('') and len(str) giving different output?
(3 answers)
Closed 2 years ago.
The count function of strings return the number of non-overlapping occurrences of substring. However, when I try to count empty string in non-empty or empty string, it does not give 0, but len(str) + 1.
>>> 'aaa'.count('') # it should have been 0
>>> 4
>>> ''.count('') # it should have been 0
>>> 1
What is the logic behind this?
That's because empty strings are considered to exist between all the
characters of a string; for a string length 2, there are 3 empty
strings; one at the start, one between the two characters, and one at
the end.
original answer
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
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','')
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