Home > Software engineering >  Get n Characters from String specified in python
Get n Characters from String specified in python

Time:10-29

Let s = "*BCDE" - get first 2 characters from string then output should be "AB". Need to get characters from string specified by numeral in string. E.g. s1="ABCDERTYUIOPLKHGF" - get first 12 characters from string.

I tried to get digit using re.findall('\d ', string ), but this creates problem if my string would be "*BCD1". Please suggest

CodePudding user response:

A simpler way of doing this would be to do the following:

txt = "*BCDE"
number_list = [s for s in txt if s.isdigit()]
number_concate = int("".join(number_list))
txt_filtered = txt[len(number_list) 1:]
print(txt_filtered[:number_concate])

Outputs AB for string "*BCDE"

Outputs ABCDERTYUIOP for string "ABCDERTYUIOPLKHGF"

You are taking your string, doing a list comprehension of the string if the digit exists, then joining the list and changing this to an integer to allow for you to filter your string accordingly. Then you strip the string to only the characters and you have your answer printed out.

CodePudding user response:

s = "*BCDE"
number = ""
offset = 0
for i in range(len(s)):
    if s[i] == "%":
        continue
    elif s[i].isdigit():
        number  = s[i]
    else:
        offset = i
        break

print(s[offset:int(number)   offset])

Output: AB

CodePudding user response:

import re
s = '*BCDERTYUIOPLKHGF'
numeral_instruction = re.search(r'%\d ',s).group(0)
start = len(numeral_instruction)
stop = start   int(numeral_instruction[1:])
s[start:stop]

outputs

AB

CodePudding user response:

You can get the position of the first non-digit character in the string (skipping the %) and use that as the basis to get the length and form a substring:

s1="ABCDERTYUIOPLKHGF"

i = next(i for i,c in enumerate(s1[1:],1) if not c.isdigit())
result = s1[i:i int(s1[1:i])]

print(result)
ABCDERTYUIOP

If the first number in the string is not always at index 1, you can skip a variable number of characters using the same technique:

s1="*:ABCDERTYUIOPLKHGF"

start = next(i for i,c in enumerate(s1) if c.isdigit())           
end   = next(i for i,c in enumerate(s1[start:],start) if not c.isdigit())
result = s1[end:end int(s1[start:end])]

print(result)
ABCDERTYUIOP

If you want to use the re module, you only need the first occurrence of a number, so use re.search() instead of re.findall():

import re
m      = re.search(r"\d ",s1)
result = s1[m.end():m.end() int(m.group())]

print(result)
ABCDERTYUIOP
  • Related