
In Python, le stringhe con prefisso ro R, come r’…’ e r”…”, sono chiamate stringhe grezze e trattano le barre rovesciate come caratteri letterali. Le stringhe non elaborate sono utili si gestiscono stringhe che utilizzano molte barre quando inverse, come percorsi Windows e modelli di espressioni regolari.
In questo articolo vengono descritti i seguenti contenuti.
- Sequenze di fuga
- Le stringhe grezze trattano le barre inverse come caratteri letterali
- Converti stringhe normali in stringhe grezze con repr()
- Le stringhe grezze non possono terminare con un numero dispari di barre inverse
Sequenze di fuga
In Python, i caratteri che non possono essere rappresentati in una stringa normale (come tabulazioni, avanzamenti di riga, ecc.) sono descritti utilizzando una sequenza di escape con una barra rovesciata (come ton), simile al linguaggio C.
s = 'atbnAtB'
print(s)
# a b
# A B
Le stringhe grezze trattano le barre inverse come caratteri letterali
Le stringhe precedute da ro R, come r’…’ e r”…”, sono chiamate stringhe grezze e trattano le barre rovesciate come caratteri letterali. Nelle stringhe grezze, le sequenze di escape non vengono trattate in modo speciale.
rs = r'atbnAtB'
print(rs)
# atbnAtB
Non esiste un tipo speciale per le stringhe grezze; è solo una stringa, che equivale a una stringa normale con barre inverse rappresentate da \.
print(type(rs))
# <class 'str'>
print(rs == 'a\tb\nA\tB')
# True
In una stringa, una normale sequenza di escape è considerato un carattere, ma in una stringa grezza anche le barre rovesciate vengono conteggiate come.
print(len(s))
# 7
print(list(s))
# ['a', 't', 'b', 'n', 'A', 't', 'B']
print(len(rs))
# 10
print(list(rs))
# ['a', '\', 't', 'b', '\', 'n', 'A', '\', 't', 'B']
Percorsi di Windows
L’uso della stringa grezza è quando utile si rappresenta un percorso di Windows come una stringa.
I percorsi di Windows sono separati da barre rovesciate , quindi se usi una stringa normale, devi eseguire l’escape di ciascuna come \, ma puoi scriverla così com’è con una stringa grezza.
path = 'C:\Windows\system32\cmd.exe'
rpath = r'C:Windowssystem32cmd.exe'
print(path == rpath)
# True
Si noti che una stringa che termina con un numero dispari di barre inverse generi un errore, come di seguito. In questo caso, è necessario scriverlo in una stringa normale o scrivere solo la barra rovesciata finale come stringa normale e concatenarla.
path2 = 'C:\Windows\system32\'
# rpath2 = r'C:Windowssystem32'
# SyntaxError: EOL while scanning string literal
rpath2 = r'C:Windowssystem32' + '\'
print(path2 == rpath2)
# True
Converti stringhe normali in stringhe grezze con repr()
Utilizzare la funzione incorporata repr() per le stringhe normali in stringhe grezze.
s_r = repr(s)
print(s_r)
# 'atbnAtB'
La stringa restituita da repr() ha ‘all’inizio e alla fine.
print(list(s_r))
# ["'", 'a', '\', 't', 'b', '\', 'n', 'A', '\', 't', 'B', "'"]
Usando le fette, puoi ottenere la stringa equivalente alla stringa grezza.
s_r2 = repr(s)[1:-1]
print(s_r2)
# atbnAtB
print(s_r2 == rs)
# True
print(r't' == repr('t')[1:-1])
# True
Le stringhe grezze non possono terminare con un numero dispari di barre inverse
Poiché le barre inverse sfuggono a ‘o “, si verificherà un errore se è presente un numero dispari di barre inverse alla fine della stringa.
# print(r'')
# SyntaxError: EOL while scanning string literal
print(r'\')
# \
# print(r'\')
# SyntaxError: EOL while scanning string literal
