[Python] What's the difference between r'string' and normal 'string' in Python?

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    What's the difference between an r string (r'foobar') and a normal string ('foobar') in Python? Is r'string' a regex string?

    I've tried the following and there aren't any effects on my regex matches:

    >>> import re
    >>> n = 3
    >>> rgx = '(?=('+'\S'*n+'))'
    >>> x = 'foobar'
    >>> re.findall(rgx,x)
    ['foo', 'oob', 'oba', 'bar']
    >>>
    >>> rgx2 = r'(?=('+'\S'*n+'))'
    >>> re.findall(rgx2,x)
    ['foo', 'oob', 'oba', 'bar']
    >>>
    >>> rgx3 = r'(?=(\S\S\S))'
    >>> re.findall(rgx3,x)
    ['foo', 'oob', 'oba', 'bar']

    Continue reading...

Compartilhe esta Página