Extracting parameters from a Python F-string












0















I have a python f-string as follows:



def send_string():
ticket_id=123
message = f'{ticket_id} Jira created successfully'
return message

def extract_ticket_from_message(message):
#pseudo code
# Is it possible to extract the ticket id without parsing the
# whole string and using regex


Is there a convenient way of extracting the ticket_id value from the f-string without having to parse the whole string using regex in Python 3.6?










share|improve this question




















  • 2





    Well, the variable ticket_id should hold it. I'm guessing that's not what you're looking for... can you expand a bit? Maybe with a little more example code?

    – glibdud
    Nov 15 '18 at 18:23











  • is "ticket_id" literally the expected output in your example, or are you expecting a ticket id like "1234"? Does the solution need to be generalized? Do we have any garauntees about what ticket_id is going to be: i.e.- do we know it won't contain spaces and you can simply use message.split(maxsplit=1)[0]?

    – Reid Ballard
    Nov 15 '18 at 18:23











  • Why not simply yourDesiredResult = str(ticket_id)? Is this an XY problem?

    – usr2564301
    Nov 15 '18 at 18:26













  • @usr2564301, Why not yourDesiredResult = ticket_id?

    – Austin
    Nov 15 '18 at 18:28











  • @Austin: in the formatted f-string it will be a string. It looks like OP is thinking about alternatives to getting its value as a string. We will have to await clarification, though.

    – usr2564301
    Nov 15 '18 at 18:30
















0















I have a python f-string as follows:



def send_string():
ticket_id=123
message = f'{ticket_id} Jira created successfully'
return message

def extract_ticket_from_message(message):
#pseudo code
# Is it possible to extract the ticket id without parsing the
# whole string and using regex


Is there a convenient way of extracting the ticket_id value from the f-string without having to parse the whole string using regex in Python 3.6?










share|improve this question




















  • 2





    Well, the variable ticket_id should hold it. I'm guessing that's not what you're looking for... can you expand a bit? Maybe with a little more example code?

    – glibdud
    Nov 15 '18 at 18:23











  • is "ticket_id" literally the expected output in your example, or are you expecting a ticket id like "1234"? Does the solution need to be generalized? Do we have any garauntees about what ticket_id is going to be: i.e.- do we know it won't contain spaces and you can simply use message.split(maxsplit=1)[0]?

    – Reid Ballard
    Nov 15 '18 at 18:23











  • Why not simply yourDesiredResult = str(ticket_id)? Is this an XY problem?

    – usr2564301
    Nov 15 '18 at 18:26













  • @usr2564301, Why not yourDesiredResult = ticket_id?

    – Austin
    Nov 15 '18 at 18:28











  • @Austin: in the formatted f-string it will be a string. It looks like OP is thinking about alternatives to getting its value as a string. We will have to await clarification, though.

    – usr2564301
    Nov 15 '18 at 18:30














0












0








0








I have a python f-string as follows:



def send_string():
ticket_id=123
message = f'{ticket_id} Jira created successfully'
return message

def extract_ticket_from_message(message):
#pseudo code
# Is it possible to extract the ticket id without parsing the
# whole string and using regex


Is there a convenient way of extracting the ticket_id value from the f-string without having to parse the whole string using regex in Python 3.6?










share|improve this question
















I have a python f-string as follows:



def send_string():
ticket_id=123
message = f'{ticket_id} Jira created successfully'
return message

def extract_ticket_from_message(message):
#pseudo code
# Is it possible to extract the ticket id without parsing the
# whole string and using regex


Is there a convenient way of extracting the ticket_id value from the f-string without having to parse the whole string using regex in Python 3.6?







python string python-3.x f-string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:35







Amistad

















asked Nov 15 '18 at 18:15









AmistadAmistad

2,74472639




2,74472639








  • 2





    Well, the variable ticket_id should hold it. I'm guessing that's not what you're looking for... can you expand a bit? Maybe with a little more example code?

    – glibdud
    Nov 15 '18 at 18:23











  • is "ticket_id" literally the expected output in your example, or are you expecting a ticket id like "1234"? Does the solution need to be generalized? Do we have any garauntees about what ticket_id is going to be: i.e.- do we know it won't contain spaces and you can simply use message.split(maxsplit=1)[0]?

    – Reid Ballard
    Nov 15 '18 at 18:23











  • Why not simply yourDesiredResult = str(ticket_id)? Is this an XY problem?

    – usr2564301
    Nov 15 '18 at 18:26













  • @usr2564301, Why not yourDesiredResult = ticket_id?

    – Austin
    Nov 15 '18 at 18:28











  • @Austin: in the formatted f-string it will be a string. It looks like OP is thinking about alternatives to getting its value as a string. We will have to await clarification, though.

    – usr2564301
    Nov 15 '18 at 18:30














  • 2





    Well, the variable ticket_id should hold it. I'm guessing that's not what you're looking for... can you expand a bit? Maybe with a little more example code?

    – glibdud
    Nov 15 '18 at 18:23











  • is "ticket_id" literally the expected output in your example, or are you expecting a ticket id like "1234"? Does the solution need to be generalized? Do we have any garauntees about what ticket_id is going to be: i.e.- do we know it won't contain spaces and you can simply use message.split(maxsplit=1)[0]?

    – Reid Ballard
    Nov 15 '18 at 18:23











  • Why not simply yourDesiredResult = str(ticket_id)? Is this an XY problem?

    – usr2564301
    Nov 15 '18 at 18:26













  • @usr2564301, Why not yourDesiredResult = ticket_id?

    – Austin
    Nov 15 '18 at 18:28











  • @Austin: in the formatted f-string it will be a string. It looks like OP is thinking about alternatives to getting its value as a string. We will have to await clarification, though.

    – usr2564301
    Nov 15 '18 at 18:30








2




2





Well, the variable ticket_id should hold it. I'm guessing that's not what you're looking for... can you expand a bit? Maybe with a little more example code?

– glibdud
Nov 15 '18 at 18:23





Well, the variable ticket_id should hold it. I'm guessing that's not what you're looking for... can you expand a bit? Maybe with a little more example code?

– glibdud
Nov 15 '18 at 18:23













is "ticket_id" literally the expected output in your example, or are you expecting a ticket id like "1234"? Does the solution need to be generalized? Do we have any garauntees about what ticket_id is going to be: i.e.- do we know it won't contain spaces and you can simply use message.split(maxsplit=1)[0]?

– Reid Ballard
Nov 15 '18 at 18:23





is "ticket_id" literally the expected output in your example, or are you expecting a ticket id like "1234"? Does the solution need to be generalized? Do we have any garauntees about what ticket_id is going to be: i.e.- do we know it won't contain spaces and you can simply use message.split(maxsplit=1)[0]?

– Reid Ballard
Nov 15 '18 at 18:23













Why not simply yourDesiredResult = str(ticket_id)? Is this an XY problem?

– usr2564301
Nov 15 '18 at 18:26







Why not simply yourDesiredResult = str(ticket_id)? Is this an XY problem?

– usr2564301
Nov 15 '18 at 18:26















@usr2564301, Why not yourDesiredResult = ticket_id?

– Austin
Nov 15 '18 at 18:28





@usr2564301, Why not yourDesiredResult = ticket_id?

– Austin
Nov 15 '18 at 18:28













@Austin: in the formatted f-string it will be a string. It looks like OP is thinking about alternatives to getting its value as a string. We will have to await clarification, though.

– usr2564301
Nov 15 '18 at 18:30





@Austin: in the formatted f-string it will be a string. It looks like OP is thinking about alternatives to getting its value as a string. We will have to await clarification, though.

– usr2564301
Nov 15 '18 at 18:30












1 Answer
1






active

oldest

votes


















1














If



 ticket_id = 1234
message = f'{ticket_id} Jira created successfully'


then – without using a regex –



def extract_ticket_from_message(message):
return message.split()[0]


In other words, the first word. If ticket_id can be any string as well (so possibly containing spaces), you can still use this but cut off the final 3 words instead. (After all, you know what will follow.) If ticket_id is a more complex object that results in a string representation, there is no practical way to resolve it back to the original class/object/anything else than a Python primitive.



Noteworthy: you cannot get the original type without ambiguity. If the original was a string but its value was "1234", then you cannot know for sure if a string or number was passed.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325587%2fextracting-parameters-from-a-python-f-string%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If



     ticket_id = 1234
    message = f'{ticket_id} Jira created successfully'


    then – without using a regex –



    def extract_ticket_from_message(message):
    return message.split()[0]


    In other words, the first word. If ticket_id can be any string as well (so possibly containing spaces), you can still use this but cut off the final 3 words instead. (After all, you know what will follow.) If ticket_id is a more complex object that results in a string representation, there is no practical way to resolve it back to the original class/object/anything else than a Python primitive.



    Noteworthy: you cannot get the original type without ambiguity. If the original was a string but its value was "1234", then you cannot know for sure if a string or number was passed.






    share|improve this answer






























      1














      If



       ticket_id = 1234
      message = f'{ticket_id} Jira created successfully'


      then – without using a regex –



      def extract_ticket_from_message(message):
      return message.split()[0]


      In other words, the first word. If ticket_id can be any string as well (so possibly containing spaces), you can still use this but cut off the final 3 words instead. (After all, you know what will follow.) If ticket_id is a more complex object that results in a string representation, there is no practical way to resolve it back to the original class/object/anything else than a Python primitive.



      Noteworthy: you cannot get the original type without ambiguity. If the original was a string but its value was "1234", then you cannot know for sure if a string or number was passed.






      share|improve this answer




























        1












        1








        1







        If



         ticket_id = 1234
        message = f'{ticket_id} Jira created successfully'


        then – without using a regex –



        def extract_ticket_from_message(message):
        return message.split()[0]


        In other words, the first word. If ticket_id can be any string as well (so possibly containing spaces), you can still use this but cut off the final 3 words instead. (After all, you know what will follow.) If ticket_id is a more complex object that results in a string representation, there is no practical way to resolve it back to the original class/object/anything else than a Python primitive.



        Noteworthy: you cannot get the original type without ambiguity. If the original was a string but its value was "1234", then you cannot know for sure if a string or number was passed.






        share|improve this answer















        If



         ticket_id = 1234
        message = f'{ticket_id} Jira created successfully'


        then – without using a regex –



        def extract_ticket_from_message(message):
        return message.split()[0]


        In other words, the first word. If ticket_id can be any string as well (so possibly containing spaces), you can still use this but cut off the final 3 words instead. (After all, you know what will follow.) If ticket_id is a more complex object that results in a string representation, there is no practical way to resolve it back to the original class/object/anything else than a Python primitive.



        Noteworthy: you cannot get the original type without ambiguity. If the original was a string but its value was "1234", then you cannot know for sure if a string or number was passed.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 10:28

























        answered Nov 15 '18 at 18:34









        usr2564301usr2564301

        17.9k73372




        17.9k73372
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325587%2fextracting-parameters-from-a-python-f-string%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma