Question: Example strings:

`myString1 = "/desktop/2512754353/Screenshots/photo_0000.png"`

`myString2 = "/desktop/51232132561/Screenshots/photo_3501.png"`

`myString3 = "/desktop/12321516123/Screenshots/photo_7501.png"`

`myString4 = "/desktop/5234324324/Screenshots/photo_11501.png"`

I had a look around, and couldn't really figure out a proper way to do this. I want to be able to also retrieve the last numbers of my strings after the **photo\_** part, and store them in another variable (string, not int or float). Furthermore, I don't need the number before **/Screenshots**. It would also be nice if it can work for any number length. The **photo\_** will always remain inside the string too.

Answer: 
You can write a regex that only matches the end of the string

```
>>> import re
>>> myString1 =  "/desktop/2512754353/Screenshots/photo_0000.png"
>>> re.search(r"photo_(\d+)\.png$", myString1).group(1)
'0000'

```
You can use [pathlib](https://docs.python.org/3/library/pathlib.html) and split:

```
from pathlib import Path 

fn="/desktop/5234324324/Screenshots/photo_11501.png"
Path(fn).stem.split('_')[-1])

# 11501

```

The pathlib property [.stem](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.stem) is the name of the path stripped of the path to it and the extension:

```
>>> Path(fn).stem
'photo_11501'

```

Then either split or partition on the `'_'` delimiter:

```
>>> Path(fn).stem.partition('_')
('photo', '_', '11501')
>>> Path(fn).stem.split('_')
['photo', '11501']

```

You can use `split` or `partition` entirely on strings that represent paths:

```
>>> fn.partition('.png')[0].partition('_')[-1]
'11501'

```

But using pathlib allows you to produce those paths as the result of a glob or other method and is likely more robust and certainly more cross platform.