S
S
Suohit2020-08-02 20:58:14
Python
Suohit, 2020-08-02 20:58:14

How to remove the contents of a string after a certain character that is repeated there several times?

It is required to delete the contents of a string after a certain character that occurs several times. That is, there is a path to the file and you need to delete everything after, for example, the 4th slash.
C:\Users\username\AppData\WhatToDelete

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-08-02
@Suohit

path = 'C:\\Users\\username\\AppData\\ТоЧтоНужноУдалить'
print('\\'.join(path.split('\\')[0:4]) + '\\')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question