[Python] Get Python string value as printed by "print()"

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

  1. Stack

    Stack Membro Participativo

    Suppose I have a string as follows (after parsing some stdout):

    >>> mystr
    '--More-- \r \rSerial-Number: 12345'


    I notice that print() function automatically processes the \r characters, essentially getting rid of the entire "--More--" part, like this:

    >>> print(mystr)
    Serial-Number: 12345


    How can I achieve the same result in a variable? In other words, I'd like to get a string variable newStr, so that:

    >>> newStr
    Serial-Number: 12345

    Continue reading...

Compartilhe esta Página