'How to print character from a specific character to a specific character with multiple instances of the character?

I want to know if there's any way that we can print text starting from a specific character to a specific character. (both are not same)

The conditions:

  • There are multiple instances of start character, so i just want the first instances index to be start index.
  • The start index and end index are not same, for example "0" is start index and "()" is the end index.
  • Also there might be multiple instances of the start character.

What have I tried?:

  • I have tried string slicing
a="ahklanfatga0-gja0-gmgaf9hatn.pdfkaufs"
indexes=[i for i,j in enumerate(a) if j=="-"]

but I have a different start character and end character, so this doesn't work.

For example:

a3="ahklanfatga0-gja0-gmgaf9hatn.pdfkaufs" # start char="-", end char=".pdf"

expected_output="gja0-gmgaf9hatn.pdf"


Solution 1:[1]

If you want to start from the first occurence of -, you can use the following:

s = "ahklanfatga0-gja0-gmgaf9hatn.pdfkaufs"
#                 ^^^^^^^^^^^^^^^^^^^

preprefix, suffix, _ = s.partition(".pdf")
*_, prefix = preprefix.split("-", 1)

print(f'{prefix}{suffix}')
# Outputs gja0-gmgaf9hatn.pdf

If you want to start from the last occurence instead, you can just change the above line

*_, prefix = preprefix.split("-", 1)

to

*_, prefix = preprefix.split("-")

This modification will output now gmgaf9hatn.pdf.

Solution 2:[2]

I'm going to expand on my comment, because this looks wrong. file.readlines() returns a list, which you then convert to a string, but then split at , anyway. If you don't need other lines, why read them in the first place?

Are you sure that a3 isn't ahklanfatga0-gja0-gmgaf9hatn.pdf,kaufs, which is what it would be after running your code and files.txt being

ahklanfatga0-gja0-gmgaf9hatn.pdf
kaufs

which would make a lot more sense.

Even if files.txt only contains one line, and that line is

ahklanfatga0-gja0-gmgaf9hatn.pdfkaufs

it doesn't make sense to cast it to a string first. Instead read the first line, and modify it appropriately.

In your question left doesn't contain ".pdf" at the end, but in @enzo's answer it does, so I'm not too sure what you want. It would help if you showed what the file looks like, and the output you expect, but here's what I would do:

with open("files.txt") as f:
    for line in f:
        start = line.find("-")  # use .rfind("-") for the last index
        end = line.find(".pdf")
        if start > -1 and end > -1:
            print(line[start+1:end])

This would do it for all lines. If just for the first line, you could do:

with open("files.txt") as f:
    line = next(f)
    start = line.find("-")  # use .rfind("-") for the last index
    end = line.find(".pdf")
    if start > -1 and end > -1:
        print(line[start+1:end])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 blueteeth