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

412
Visualizações
Python: Too long raw string, multiple lines

I am using python on windows and the path of my project folder is way too long. For example:

pathProject = r'C:\Users\Account\OneDrive\Documents\Projects\2016\Shared\Project-1\Administrative\Phase-1\Final'

os.chdir(pathProject)

How can I break this very long strong into multiple lines in the most elegant way? I know how to do it if the string is not a raw string. However, if I try something like this, I get an error:

pathProject = r'''C:\Users\Account\OneDrive\
                Documents\Projects\2016\Shared\
                Project-1\Administrative\Phase-1\
                Final'''

What is the most elegant way to break this raw string into multiple lines?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can use parenthesis to trigger automatic line continuation. The strings will be automatically concatenated.

pathProject = (r'C:\Users\Account\OneDrive'
               r'\Documents\Projects\2016\Shared'
               r'\Project-1\Administrative\Phase-1\Final')
over 4 years ago · Santiago Trujillo Relatório

0

You almost got it! The issue is that raw strings cannot end with a backslash. Hence, this works:

pathProject = r'''C:\Users\Account\OneDrive
\Documents\Projects\2016\Shared
\Project-1\Administrative\Phase-1
\Final'''

Note that if you put spaces into the triple-quoted string to indent it, like in your example, there will be spaces in your string, which you don't want. If you like indents, you can use automatic line continuation with parentheses as suggested in Brendan's answer. Again, make sure that the lines don't end with a backslash.

over 4 years ago · Santiago Trujillo Relatório

0

The problem is that the ending \ is interpreted as continuation (to eliminate the LF character; Joooeey’s answer was wrong on this) and the spaces at the beginning of other lines are also included. As a single raw string, this would work:

pathProject = r'''C:\Users\Account\OneDrive\
\Documents\Projects\2016\Shared\
\Project-1\Administrative\Phase-1\
\Final'''

I think I would prefer Brendan’s way, but you probably want to know how raw strings work (or not) for your case.

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