[Python] Prepend each line in a string python

Discussão em 'Python' iniciado por Stack, Agosto 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have a variable that holds the below value:

    From: test@example.com
    To: user1@us.oracle.com
    Date: Thu Sep 29 04:25:45 2016
    Subject: IMAP Append Client FNBJL
    MIME-version: 1.0
    Content-type: text/plain; charset=UTF-8; format=flowed

    hocks burdock steelworks propellants resource querying sitings biscuits lectureship
    linearly crimea ghosting inelegant contingency resting fracas margate radiographic
    befoul waterline stopover two everlastingly highranking doctrine unsmilingly massproducing
    teacups litanies malachite pardon rarer glides nonbelievers humorously clonal tribunes
    micrometer paralysing splenetic constitutionalists wavings thoughtfulness herbicide
    rerolled ore overflows illicitly aerodynamics ably splittable ditching rouged bulldozer
    replayed statistic reconfigured adventurers passionate rewarded decides oxygenated


    Every line in the above string needs to be prepended with X: as shown below

    X: From: test@example.com
    X: To: user1@us.oracle.com
    X: Date: Thu Sep 29 04:25:45 2016
    X: Subject: SSSSSSSSS FNBJL
    X: MIME-version: 1.0
    X: Content-type: text/plain
    X:
    X: hocks burdock steelworks propellants resource querying sitings biscuits lectureship
    X: linearly crimea ghosting inelegant contingency resting fracas margate radiographic
    X: befoul waterline stopover two everlastingly highranking doctrine unsmilingly massproducing
    X: teacups litanies malachite pardon rarer glides nonbelievers humorously clonal tribunes
    X: micrometer paralysing splenetic constitutionalists wavings thoughtfulness herbicide
    X: rerolled ore overflows illicitly aerodynamics ably splittable ditching rouged bulldozer
    X: replayed statistic reconfigured adventurers passionate rewarded decides oxygenated


    I was thinking of splitting the above string on \n and prepending each line with X:

    Is there a better approach for this?

    Continue reading...

Compartilhe esta Página