Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
Trim specific leading and trailing characters from a string

I have a string that contains a path

str = "/example/path/with/different/trailing/delimiter\"

and I want to trim the leading and trailing / and \. What is the best practice in Python 3?

Currently I'm using

trimmedPath = str.strip("/\\")
# trimmedPath is "example/path/with/different/trailing/delimiter" as desired

Two questions:

  1. Is this the best trim function for trimming specific characters in Python 3?
  2. Are there specific path functions for such operations in Python 3 so I don't have to set the delimiters manually?
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I believe strip is the pythonic way. It is usually the case when there is a builtin function.

There are a few builtin path manipulators available in the os library. You might want to use them if one of the manipulators is a match for your use case.

over 4 years ago · Santiago Trujillo Relatório

0

Example of strip() in action; in this case, removing a leading plus sign:

In [1]: phone_number = "+14158889999"

In [2]: phone_number.strip('+')
Out[2]: '14158889999'
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda