• str.replace() when str contains \

    From Bernard LEDRU@21:1/5 to All on Sat Oct 29 20:21:46 2022
    Hello,

    https://docs.python.org/3/library/stdtypes.html#string-methods

    str.replace(old, new[, count])¶
    Return a copy of the string with all occurrences of substring old
    replaced by new. If the optional argument count is given, only the first
    count occurrences are replaced.

    Attention when the string contains the escape character.

    Consider :

    a="H:\2023"; print(a.replace("\\","/"))
    H:3
    a="H:\a2023"; print(a.replace("\\","/"))
    H:2023
    a="H:\_2023"; print(a.replace("\\","/"))
    H:/_2023

    Best regards,
    Bernard LEDRU

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry@21:1/5 to All on Sat Oct 29 22:27:38 2022
    On 29 Oct 2022, at 22:14, Bernard LEDRU <python@bernardledru.be> wrote:

    Hello,

    https://docs.python.org/3/library/stdtypes.html#string-methods

    str.replace(old, new[, count])¶
    Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

    Attention when the string contains the escape character.

    Consider :

    a="H:\2023"; print(a.replace("\\","/"))
    H:3
    a="H:\a2023"; print(a.replace("\\","/"))
    H:2023
    a="H:\_2023"; print(a.replace("\\","/"))
    H:/_2023

    String a does not quote the \ after :.
    You need “H:\\2023” etc. check what a is before the replace with a print(repr(a))

    Barry


    Best regards,
    Bernard LEDRU
    --
    https://mail.python.org/mailman/listinfo/python-list

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Bernard LEDRU on Sat Oct 29 18:14:49 2022
    Better to use raw strings whenever backslashes are involved.

    On 10/29/2022 2:21 PM, Bernard LEDRU wrote:
    Hello,

    https://docs.python.org/3/library/stdtypes.html#string-methods

    str.replace(old, new[, count])¶
    Return a copy of the string with all occurrences of substring old
    replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

    Attention when the string contains the escape character.

    Consider :

    a="H:\2023"; print(a.replace("\\","/"))
    H:3
    a="H:\a2023"; print(a.replace("\\","/"))
    H:2023

    These examples actually produce an "H" with a symbol after it, a
    different symbol for each. Probably the message posting system can't
    display it, but I see it in a python terminal session on Windows. So
    the first a="H:\2023" example produces "Hx", where x is some symbol.
    Since there is no backslash, no character gets replaced. IOW,

    a="H:\2023"; print(a)
    and
    a="H:\2023"; print(a.replace("\\","/"))
    print the same thing.

    a="H:\_2023"; print(a.replace("\\","/"))
    H:/_2023

    What did you expect or want to happen?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan Stromberg@21:1/5 to All on Sat Oct 29 16:06:51 2022
    I believe you would do well to print a, before trying to transform it into something else.

    '\2' is chr(2).
    '\a' is the bell character, and is unprintable.
    '\_' is two characters though.

    On Sat, Oct 29, 2022 at 2:12 PM Bernard LEDRU <python@bernardledru.be>
    wrote:

    Hello,

    https://docs.python.org/3/library/stdtypes.html#string-methods

    str.replace(old, new[, count])¶
    Return a copy of the string with all occurrences of substring old
    replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

    Attention when the string contains the escape character.

    Consider :

    a="H:\2023"; print(a.replace("\\","/"))
    H:3
    a="H:\a2023"; print(a.replace("\\","/"))
    H:2023
    a="H:\_2023"; print(a.replace("\\","/"))
    H:/_2023

    Best regards,
    Bernard LEDRU
    --
    https://mail.python.org/mailman/listinfo/python-list


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Bernard LEDRU on Sun Oct 30 01:25:32 2022
    On 2022-10-29 19:21, Bernard LEDRU wrote:
    Hello,

    https://docs.python.org/3/library/stdtypes.html#string-methods

    str.replace(old, new[, count])¶
    Return a copy of the string with all occurrences of substring old
    replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

    Attention when the string contains the escape character.

    Consider :

    a="H:\2023"; print(a.replace("\\","/"))
    H:3
    a="H:\a2023"; print(a.replace("\\","/"))
    H:2023
    a="H:\_2023"; print(a.replace("\\","/"))
    H:/_2023

    In a plain string literal, \ followed by 1..3 of the digits 0..7 is an
    octal (base 8) escape sequence, and \ followed by 'a' is the bel
    character, so '\7' == '\07' == '\007' == '\a' == '\x07'.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)